I've worked up an extension module to add "(?<name> ...)"
patterns to Perl's regex language.
These are just ordinary capturing parens, but after a
successful match you can use $&{name} instead of $1 (or
whichever $N).
print "Mmmm... $&{donuts}."
if /(?<donuts> glazed|jelly)/;
There's also a pragma to make the named groups available
as implicitly-declared 'my' variables.
use Regexp::Capture;
use strict;
while (<>) {
use capture;
next unless /(?<red> rub(i|l)es)/;
and /(?<green> sea(attle)?/;
## $1 eq $green, but:
print "Matched [$red] and [$green]\n";
}
This is just a syntax-sugar I saw in the (cough) .NET regex
library, but it's kind of fun.
Couple of questions:
The XS loads patched versions of regcomp/regexec, which the
core calls via-function-pointer. C<re 'debug'> does the
same thing (and I think this is less subversive than source
filters :-) but it probably still needs some kind of "CAVEAT".
How dire should the warning be? "Experimental?" "ACME?"
And can anybody suggest a better name/namespace?
Originally leaned toward this:
# turn on the extended patterns
use re::capture;
# turn on 'my' variables in the current scope
use re::capture 'lexical';
# make them writable
use re::capture 'copy';
But 're' is core (and the colons are chunky).
Thanks
--
Steve