3.2.160 studystudy This function takes extra time to study The way study
works is this: a linked list of every character in the string to be
searched is made, so we know, for example, where all the " For example, here is a loop that inserts index-producing entries before any line containing a certain pattern: while (<>) {
study;
print ".IX foo\n" if /\bfoo\b/;
print ".IX bar\n" if /\bbar\b/;
print ".IX blurfl\n" if /\bblurfl\b/;
...
print;
}In searching for If you have to look for strings that you don't know until run-time, you can
build an entire loop as a string and eval
that to avoid recompiling all your patterns all the time. Together with setting
$/ to input entire files as one record, this can
be very fast, often faster than specialized programs like
fgrep. The following scans a list of files
( $search = 'while (<>) { study;';
foreach $word (@words) {
$search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n";
}
$search .= "}";
@ARGV = @files;
undef $/; # slurp each entire file
eval $search; # this screams
die $@ if $@; # in case eval failed
$/ = "\n"; # put back to normal input delim
foreach $file (sort keys(%seen)) {
print $file, "\n";
} |