open my $fh, '<', $filename or die "$filename: $!"; while (<$fh>) { if (/pattern/) { # do something ... print $_; } } close $fh;