I want to find a line in a file using regex, inside a Perl script.
Assuming it is on a system with grep installed, it is better:
grep
open()
while
if ($line =~ m/regex/)
In a modern Perl implementation, regexp should be as fast as grep, but if you are concerned about performance, why not just give it a try? In terms of code cleanliness and reliability, invoking an external command-line tool is definitely not very good.
open.
open
my $regex = qr/blah/; while (<>) { if (/$regex/) { print; exit; } } print "Not found\n";
, , , print $_, <> -, .
print
$_
<>
while (my $line = <>) { if ($line =~ /$regex/) { print $line; exit; } }
.
: , , , .
grep: Linux, LANG UTF-8 (, my - LANG = en_GB.UTF-8), grep, sed, sort , , 10 . , . grep:
LANG= LANGUAGE= /bin/grep
: , 100
, Perl, .
. ,
$line = `grep '$regex' file | head -n 1`;
- , .
, , perl, , - .
sed '/pattern/q' file
script ( 10 ). Perl , . grep script, . , , Perl , grep. , , . : , .