study 
study SCALAR
studyThis function takes extra time to study
SCALAR in anticipation of doing many pattern
matches on the string before it is next modified. This may or may not
save time, depending on the nature and number of patterns you are
searching on, and on the distribution of character frequencies in the
string to be searched—you probably want to compare runtimes with and
without it to see which runs faster. Those loops that scan for many
short constant strings (including the constant parts of more complex
patterns) will benefit most from study. If all your pattern matches are
constant strings anchored at the front, study won’t help at all because no scanning is
done. You may have only one study
active at a time—if you study a different scalar, the first is
“unstudied”.
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 “k” characters are. From each search string,
the rarest character is selected, based on some static frequency tables
constructed from some C programs and English text. Only those places
that contain this rarest character are examined.
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 /\bglarch\b/; ... print; ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access