use strict; use warnings; my @txtfiles = glob "*.txt"; my @criticalfiles; for my $file (@txtfiles) { my $fh; if( not open $fh, "<", $file ) { print "Cannot open file $file, skipping.\n"; next; } while( <$fh> ) { if( /critical/ ) { push @criticalfiles, $file; last; } } close $fh; } print "Critical files:\n"; print join( "\n", @criticalfiles ), "\n";