Perl Efficiency Issues
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:
- There’s More Than One Way To Do It Perl is a toolbox offering many approaches to a solution. Knowing which problems are nails comes with understanding The Perl Way, and knowing which hammer to use for any particular nail goes a long way toward making more efficient and more understandable programs. Sometimes efficiency and understandability seem to be mutually exclusive, but a better understanding allows you to make better choices.
- Regex Compilation,
qr/···/
, the/o
Modifier, and Efficiency The interpolation and compilation of regex operands are fertile ground for saving time. The/o
modifier, which I haven’t discussed much yet, along with regex objects (qr/···/
), gives you some control over when the costly re-compilation takes place. - The
$&
Penalty The three match side effect variables,$'
,$&
, and$'
, 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. - The Study Function Since ages ...
Get Mastering Regular Expressions, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.