questionar questionar - 2 years ago 161
Perl Question

Tie file not working for loops

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;
}


the output is

Not working at tiereplacer.pl line 22.
/home/idiotonperl/main/content.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’


it’s working as intended

Answer Source

Working fine for me:

#!/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;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download