Hello again Anonymous Monk,
You are welcome :)
Update: Sorry I got your last question completely wrong :). You can repeat the same procedure as you open the files and instead of @ARGV use @files.
Sample of code:
#!/usr/bin/perl
use strict;
use warnings;
use IO::All;
use Data::Dumper;
my @files = io('fileName.txt')->chomp->slurp;
print Dumper \@files;
__END__
$ perl test.pl
$VAR1 = [
'a.txt',
'b.txt',
'c.txt'
];
Update2: I think I finally understood what you mean. You want to provide a file as a parameter to the script that contains the names of the files that you want to search through for the keyword. If this is the case see bellow:
#!/usr/bin/perl
use strict;
use warnings;
use IO::All;
use Data::Dumper;
sub grepFileFromSubroutine {
my @files = io(shift)->chomp->slurp;
my @final;
foreach my $file (@files) {
if ( -e $file ) { # double check existence
my @lines = io($file)->chomp->slurp; # Chomp as you slurp
if (my @matches = grep( /string/, @lines ) ) {
push @final, "Found in File: '$file':", @matches;
}
}
}
return \@final;
}
print Dumper grepFileFromSubroutine(@ARGV);
__END__
$ perl test.pl fileName.txt
$VAR1 = [
'Found in File: \'a.txt\':',
'Line two interested, string',
'Found in File: \'b.txt\':',
'Line one interested, string',
'Line two interested, string'
];
__DATA__
$ cat a.txt b.txt c.txt
Line one not interested
Line two interested, string
Line one interested, string
Line two interested, string
Line one not interested
Line two not interested
Line three not interested
If still is not what you want provide us more information to assist you more :)
Regarding your last question read this past question which contains also examples and a variety of solutions (Find filename that has the pattern in a directory).
I usually prefer to use File::Find::Rule. A previously asked question with a sample of code with this module see (Re: Capturing and then opening multiple files).
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|