open FH, "<$_" or die $!; print "output for file: $_\n"; while (my $line = ) { ... if ($line =~ /\.txt/){ ... } close FH; }