领带文件不适用于循环 [英] Tie file not working for loops

查看:126
本文介绍了领带文件不适用于循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以提取目录中的所有pm文件并查找某些模式并将其更改为所需的值,我尝试了Tie :: File,但它没有查找文件内容

I have a script which pulls all the pm files in my directory and look for certain pattern and change them to desired value, i tried Tie::File but it's not looking to content of the file

use File::Find;
use Data::Dumper qw(Dumper);
use Tie::File;
my @content;
find( \&wanted, '/home/idiotonperl/project/');

sub wanted {
    push @content, $File::Find::name;
return;  
}
my @content1 = grep{$_ =~ /.*.pm/} @content;
@content = @content1;
for my $absolute_path (@content) {
    my @array='';
    print $absolute_path;
    tie @array, 'Tie::File', $absolute_path or die qq{Not working};
    print Dumper @array;
    foreach my $line(@array) {
         $line=~s/PERL/perl/g;
    }
    untie @array;
 }

输出为

 Not working at tiereplacer.pl line 22.
 /home/idiotonperl/main/content.pm

这不符合预期(查看所有pm文件的内容),如果我尝试对我家中某个测试文件的单个文件执行相同的操作,则内​​容将被替换

this is not working as intended(looking into the content of all pm file), if i try to do the same operation for some test file under my home for single file, the content is getting replaced

  @content = ‘home/idiotonperl/option.pm’

它按预期工作

推荐答案

对我来说很好:

#!/usr/bin/env perl
use common::sense;
use File::Find;
use Tie::File;

my @content;

find(\&wanted, '/home/mishkin/test/t/');

sub wanted {
    push @content, $File::Find::name;
    return;
}

@content = grep{$_ =~ /.*\.pm$/} @content;

for my $absolute_path (@content) {
    my @array='';
    say $absolute_path;
    tie @array, 'Tie::File', $absolute_path or die "Not working: $!";

    for my $line (@array) {
        $line =~ s/PERL/perl/g;
    }

    untie @array;
}

这篇关于领带文件不适用于循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆