My Favorite Modules: re

The re module provides functionality relating to Perl's regular expressions. It is either a module in the sense of potentially exporting stuff into your name space or a pragma in the sense of modifying the behavior of Perl within a lexical scope, or both, depending on how you use it.

The pragmatic functionality tweaks the regular expressions themselves in various ways:

use re 'taint';

Within the scope of this pragma, capture buffers are tainted if tainting is active and the regular expression matched a tainted value.

use re 'eval';

Within the scope of this pragma, scalars interpolated into regular expressions are allowed to contain the (?{ ... }) and (??{ ... }) constructions. These are normally disallowed because they are the moral equivalent of a stringy eval, and therefore a security risk. They are always disallowed when matching tainted data. The exception to all of this is a scalar containing a compiled Regexp, which is always allowed no matter what it contains.

use re 'strict';

This experimental (as of Perl 5.34.0) feature turns on additional checks and diagnostics when compiling regular expressions. The documentation contains warnings that no promise of backward compatibility is made, and that the interface to this functionality (whatever form it takes) may change.

use re '/flags';

The /flags are the flags that can appear at the end of a regex, and are applied by default to all regular expressions compiled in the scope of the pragma. For example, use re '/i'; makes all regular expressions in its scope case-blind by default.

use re 'debug';

This causes regular expressions compiled and executed within its scope to produce debugging output showing the compiled program and how the matching is executed.

use re 'Debug';

Basically this is use re 'debug'; with control over exactly what output is generated,

As of Perl 5.9.5, this module can also export a number of regular-expression-related functions. The following list is not exhaustive, but is simply the ones that caught my eye.

is_regexp( $ref )

This function returns true if the argument is a compiled regex, and false otherwise. Unlike ref(), this is not fooled by blessing or re-blessing.

regexp_pattern( $ref )

This function returns the pattern of a compiled regexp. It is not fooled by blessing or overloading. If called in scalar context, you get the stringification of the regexp. If called in list context, you get two elements: the regexp itself, and its modifiers separately.

regname( $name, $all )

This function returns the contents of the named capture buffer $name from the most-recent successful match. If $all is true it returns a list of all buffers with that name; otherwise it returns only the first defined buffer with that name.

regnames( $all )

This function returns the names of named captures from the last successful match. If $all is true you get the names of all named captures defined by the regexp; otherwise you get only those actually involved in the match.

Previous entries in this series:

  1. if
  2. diagnostics
  3. Term::ReadLine::Perl

Leave a comment

About Tom Wyant

user-pic I blog about Perl.