... for my $file (@files) { open my $fh, '<', $file or die $!; while (<$fh>) { if (/a particular pattern/) { print "Pattern found in $file\n"; last; } } close $fh; } ... #### ... my @handles = map { open my $fh, '<', $_; $fh } @files; for my $index (0 .. $#files) { next unless $handles[$index]->opened(); while (<$handles[$index]>) { if (/a particular pattern/) { print "Pattern found in $files[$index]\n"; last; } } } close $_ for grep { $_->opened() } @handles; ...