This is really no different that looping through one file as long as you pay attention to a few of Perl's tricks.
For one file it is common to use
use strict;
use warnings;
use English qw(-no_match_vars);
my $filename = 'foo';
open my $file, '<', $filename or die "Failed to open '$filename' $OS_ERROR\n";
while (my $line = <$file>) {
# work with $line
}
close $file;
This can be expanded to two files by opening both and changing the loop conditional to only end when both files are done reading.
But there is a catch, when Perl sees a simple read from a file handle as the conditional for a while loop it wraps it in defined()
for you, since the conditional is now more than a simple read this needs to be done manually.
use strict;
use warnings;
use English qw(-no_match_vars);
my $filename1 = 'foo';
my $filename2 = 'bar';
open my $file1, '<', $filename1 or die "Failed to open '$filename1' $OS_ERROR\n";
open my $file2, '<', $filename2 or die "Failed to open '$filename2' $OS_ERROR\n";
my ($line1, $line2);
while ( do { $line1 = <$file1>; $line2 = <$file2>; defined($line1) || defined($line2) } ) {
# do what you need to with $line1 and $line2
}
close $file1;
close $file2;