http://www.perlmonks.org?node_id=562297

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

Fellow monks,

I have a need to read N files one line at a time and then manipulate those individual lines depending on their content. I was hoping to do something like this

use strict; use warnings; use English; print "Usage: $0 output input1 input2 ...\n"; my $outfile = shift @ARGV; open(OUT, ">$outfile") || die "Can't open $outfile for writing: $!\n"; my @infile; foreach (@ARGV) { open(IN, $_) || die "can't open $_ for reading: $!\n"; push @infile, \*IN; } foreach(@infile){ my $i = <$_>; print "Got: $i"; }

where the @infile array contains all the open file handles so I could read from each one individually. Unfortunately, my test loop at the bottom seems to only read the contents of the last input file specified on the command line. I have this nagging feeling that my problem has something to do with closures (a concept I do not completely comprehend yet). What am I doing wrong?