You don't need to open
the file explicitly.
my $regex = qr/blah/;
while (<>) {
if (/$regex/) {
print;
exit;
}
}
print "Not found\n";
Since you seem concerned about performance, I let the match and print
use the default $_
provided by not assigning <>
to anything, which is marginally faster. In normal production code,
while (my $line = <>) {
if ($line =~ /$regex/) {
print $line;
exit;
}
}
would be preferred.
Edit: This assumes that the file to check is given on the command line, which I just noticed you had not stated applies in your case.