my @lines = <$fh>; # read whole file foreach (0 .. $#lines) { if ($lines[$_] =~ /$regex/) { printf "Match found on line %d\n", ($_ + 1); $match = 1; } } #### while ( <$fh> ) { if ( /$regex/ ) { print "Match found on line $.\n"; $match = 1; } } #### my $lines; while ( <$fh> ) { $lines .= $_; if ( $lines =~ /($regex)/sm ) { my $newlines = $1 =~ tr/\n//; if ( $newlines ) { print "Match found on lines ", $. - $newlines, " through $.\n"; } else { print "Match found on line $.\n"; } $match = 1; $lines = $_; } }