XML technologies / Regular expressions / Perl / Efficiency in Perl / Regex debugging information
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Perl applies many optimizations to find matches as quickly as possible. Common techniques appear under Common optimizations in The art of writing regular expressions, but many others exist. Most apply only in specific situations, so any particular regex benefits from only a few, or none.
Perl offers debugging options that report certain optimizations. When compiling a regex, Perl determines which optimizations apply and displays them in debugging mode. Debugging also reports aspects of how the engine applies the regex. Explaining all output would exceed this book’s scope; this is only a brief introduction.
Enable regex debugging with use re 'debug'; and disable it with no re 'debug';. We previously encountered the use re pragma with other arguments: use re 'eval'; allowed Perl code in interpolated variables; see the box Interpolating code patterns.
To enable debugging for the whole script, invoke it with the command-line option -Mre=debug rather than just its name. This also lets you test one regex in a one-liner; some less interesting lines are omitted here:
% perl -cw -Mre=debug -e 'm/^Subject: (.*)/'
Compiling REx `^Subject: (.*)'
rarest char j at 3
1: BOL(2)
2: EXACT <Subject: >(6)
...
12: END(0)
anchored `Subject: ' at 0 (checking anchored) anchored(BOL) minlen 9
Omitting $` $& $' support.Line 1 invokes Perl with these command-line options: -c (compile only, without executing), -w (warn about questionable constructs—always use it), and -Mre=debug (enable debugging output). After -e, the command line supplies the miniature Perl program ›m/^Subject:● (.*)/‹ .
Line 3 identifies the rarest (rarest) character in the regex’s literal substring, using a static character-frequency table. Perl uses it for optimizations such as Searching for individual characters or fixed substrings.
Lines 4–7 show the compiled internal regex representation. We will not explore it, but line 5 is fairly understandable even at a glance.
Line 8 contains several details, potentially including:
anchored `String' at Offset
Indicates that a match must contain String at position Offset from its start. If `String' is immediately followed by
›$‹, the match must end with String .
floating `String' at von . . bis
Indicates that a match must contain String , not at a fixed position but starting anywhere between von and bis . If `String' is immediately followed by
›$‹, the match must end with String .
stclass `list'
Lists possible characters with which a match must begin.
anchored(MBOL), anchored(BOL), anchored(SBOL)
The regex begins with
˹^˼. TheMBOLversion appears when the/mmodifier is active; otherwise,BOLorSBOLappears. (BOLandSBOLare identical in modern Perl;SBOLreferred to the variable$*, whose use has long been discouraged).
anchored(GPOS)
The regex begins with
˹\G˼.
implicit
The
anchored(MBOL)was implicitly added because the regex begins with˹.*˼.
implicit Länge
A match must be at least Länge characters long.
with eval
The regex contains
˹(?{...})˼or˹(??{...})˼.
Line 9 is not regex-specific and appears only if the Perl interpreter was compiled with the option -DDEBUGGING. Once the program is loaded, Perl reports whether variables such as $& are used (see The variables $`, $& and $' are naughty).
Runtime debugging information
We developed code to report matching progress at runtime (see Code patterns that report progress while matching), but Perl’s debugging options offer this and more automatically. Omitting the -c option makes Perl output extensive details about matching progress.
The message “Match rejected by optimizer” means that the transmission already determined the regex cannot match. The regex is never applied. For example:
% perl -w -Mre=debug -e fl"Nur ein Test" =~ m/^Subject:/;'
...
Did not find anchored substr `Subject:'...
Match rejected by optimizerWith debugging enabled, every regex is reported, not just your own. For example,
% perl -w -Mre=debug -e 'use warnings'
... jede Menge Debugging-Informationen ...
...merely loads the warnings module. But since that module contains regexes, their debugging information appears too.
Other ways to enable debugging information
Regex debugging can be enabled with use re 'debug'; and -Mre=debug. If debugcolor is used instead of debug and the terminal understands ANSI escape sequences, bold and similar formatting improve readability.
If the Perl interpreter was compiled with the option -DDEBUGGING, instead of -Mre=debug, use the command-line option -Dr.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln