renormalist has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I try to use the new advanced 5.9.5 regex features. I have problems to understand the usefulness of DEFINE'ing sub-patterns, because inside of them another feature, capture buffers via %+, does not work.

This would make DEFINE less fun, because structuring the regex this way and at the same time loosing the ability to reach captured parts seems quite antagonistic. So I suspect, I'm doing something wrong.

Please see the following two examples. How can I access the matched animal in the second example?

Example 1:
#! /usr/local/bin/perl5.9.5 use v5.9.5; use Data::Dumper; my $re; $re = qr/The animal is: (?<animal>monkey|tiger|lion)/; my $string = 'The animal is: tiger'; if ($string =~ $re) { say "Successfully matched."; print Dumper(\%+); }
Example 2:
#! /usr/local/bin/perl5.9.5 use v5.9.5; use Data::Dumper; my $re; $re = qr/ (?(DEFINE) (?<phrase>The \s animal \s is: \s (?<animal>monkey|tiger|lion)) ) (?&phrase) /x; my $string = 'The animal is: tiger'; if ($string =~ $re) { say "Successfully matched."; print Dumper(\%+); print Dumper(\%-); }

"man perlre" seems to confirm me:

Note that capture buffers matched inside of recursion are not accessible after the recursion returns, so the extra layer of capturing buffers is necessary. Thus $+{NAME_PAT} would not be defined even though $+{NAME} would be [see example].

But I hope there is a way to solve it somehow.

Thanks.

Replies are listed 'Best First'.
Re: 5.10 regexes with subpatterns
by demerphq (Chancellor) on Aug 12, 2007 at 09:53 UTC

    You aren't doing anything wrong, this is how it works right now. DEFINE is for situations where you want to construct something like a validation routine from a grammar.

    The issue of not being able to access named capture buffers after recursion actually applies to all capture buffers from a nested construct, such as when using (??{...}) and not just with named recursion. Its a limitation of the overall engine and its not likely to be changed or fixed anytime soon. (Especially as I have more things to do these days than I have time to do them even without considering Perl core hacking).

    It has been discussed a number of times on #p5p that it would be nice to build a parse tree from named capture rules, but its enough work that it certainly wont be done in time for 5.10.0, maybe it will be done in a latter 5.10.x or even 5.12, but I cant say for sure.

    ---
    $world=~s/war/peace/g

Re: 5.10 regexes with subpatterns
by renormalist (Sexton) on Aug 10, 2007 at 12:16 UTC
    Maybe someone can point me to other places where I can discuss such 5.10 regex problems. I would love to do grammar like stuff with them. Sometimes I would like to write such questions in perl5-porters, but I probably more disturb the original idea of that list. So, any suggestions where else I can discuss this? Or is it worth p5p?