http://qs321.pair.com?node_id=1149392

Leiria has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks!

First please allow me to apologize for my rookie question for which I seek enlightenment :)

I'm very new when it comes to programming, specially perl (which I'm enjoying very much so far)

I've got an apparent simple problem that I can't quite figure out. Here's the scenario:

- On the command line I can pass several parameters, each containing strings or files paths separated by pipes: script.pl --id1="test1|test2" --id2="test3|test4" --file="file1.txt|file2.txt"

- The files can be in several directories, so placing them all in one directory and simply reading it wouldn't be an option, I guess

- I wish to use the strings on parameters "id1" and "id2" to search all the specified files on parameter "file" and then print out the file name with the line numbers that match (or even better, output this to a brand new file)

So far, I've managed to write the following code after doing some readings, which works well if I specify only one file:

#!usr/bin/perl use strict; use warnings; use Getopt::Long; GetOptions( 'id1=s' => \my $id1, 'id2=s' => \my $id2, 'file=s' => \my $file, ); open (my $filename, "<", $file) or die "Could not open $file, $!"; while (<$filename>){ while (/$id1/g && /$id2/g) { print "$file:$.\n"; } } close ($filename);

If I run as is, with only one file specified, it works fine, except for the print part, where I'm getting globs instead of the actual filenames:

GLOB(0xa5a36c):61

GLOB(0xa5a36c):65

So moving on to the actual questions:

1. How could I loop through all the files and open them line by line and search for the matching strings?

2. How could I output the matches to a new file?

3. How could I list the actual matching file names instead of the globs?

Thank you all very much and I'm sorry if I wasn't clear enough on my intentions, for which I apologize. Any guidance or directions towards the right path are highly appreciated

With best regards,

Leiria