For the most part, efficiency with Perl regular expressions is achieved in the same way as with any tool that uses a Traditional NFA. Use the techniques discussed in Chapter 6 — the internal optimizations, the unrolling methods, the “Think” section — all apply to Perl.
There are, of course, Perl-specific issues as well, and in this section, we’ll look at the following topics:
/oModifier, and Efficiency The interpolation and compilation of regex operands are fertile ground for saving time. The
/omodifier, which I haven’t discussed much yet, along with regex objects (
qr/···/), gives you some control over when the costly re-compilation takes place.
$&Penalty The three match side effect variables,
$', can be convenient, but there’s a hidden efficiency gotcha waiting in store for any script that uses them, even once, anywhere. Heck, you don’t even have to use them — the entire script is penalized if one of these variables even appears in the script.