use strict; use warnings; print "\n"; scalar @ARGV == 2 or die "USAGE: perl $0 \n"; open(my $fh, '<', $ARGV[0]) or die "Unable to open file '$ARGV[0]' for reading: $!"; my $match = 0; my $regex = qr/$ARGV[1]/; my @lines = <$fh>; # read whole file foreach (0 .. $#lines) { if ($lines[$_] =~ /$regex/) { printf "Match found on line %d\n", ($_ + 1); $match = 1; } } print "No matches found\n" unless $match; #### >perl findinfile.pl run.c "int\s+main\s*\(" #### int main(int argc, char** argv) #### ... my $text; my $match = 0; my $regex = qr/$ARGV[1]/; { local $/; # enable "slurp" mode $text = <$fh>; # read whole file } while ($text =~ /$regex/gms) { print "Match found\n"; $match = 1; } print "No matches found\n" unless $match;