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

Is there a way to reach multiple files in Perl? For instance, I am using Compress::Zlib to read .gz files. I have two file handles <FILE1> and <FILE2>. I need to access these two files line by line at once. Thank you. P.

Replies are listed 'Best First'.
Re: Reading multiple files.
by wind (Priest) on Mar 24, 2011 at 18:24 UTC
    Just use eof in a while loop. Don't forget to test that all file handles reached their end at the same time.
    while (!eof(FILE1) && !eof(FILE2)) { my $line1 = <FILE1>; my $line2 = <FILE2>; ...
    Probably a little easier to test if you're using lexical file handles.
    use strict; use warnings; open my $fh, $0 or die $!; my @fhs = (\*DATA, $fh); while (! grep {eof($_)} @fhs) { my @lines = map {scalar <$_>} @fhs; print for @lines; } __DATA__ line1 line2 line3 line4 line5 line6
      Former works, Thank you. P.

        But you really should use lexical file handles, use three parameter open, check open results and use strictures (use strict; use warnings;). Funny how not doing all of that seems to go hand in hand, but surely you do all that good stuff. :)

        True laziness is hard work