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

Please see Python code, then the Perl equivalent:
import re r = re.compile(r'(?P<A> a (?P<B> b))', re.X) m = r.search('ab') for k in m.groupdict(): print k, '=>', m.group(k) # 'A => ab' # 'B => b' print m.group('A') # 'ab' print m.group('B') # 'b'
Perl equivalent:
'ab' =~ /(?<A> a (?<B> b))/x; print "$_ => $+{$_}\n" for (keys(%+)); # 'A => ab' # ... nothing, thats it print "$+{'A'}\n"; # 'ab' print "$+{'B'}\n"; # 'b'
Perl doesn't enumerate all the keys if a named capture is nested within another. But it it clearly holds the correct value 'b' with the correct key 'B' in the hash. Is this a "feature" or a bug? ;)

Replies are listed 'Best First'.
Re: Perl 5.10 Named Capture Bug?
by SankoR (Prior) on Jul 08, 2008 at 21:10 UTC
    I'll trust moritz that it's a feature, but it's one that has been fixed (...or removed as features don't require a fix ;) in bleadperl.
      Please don't trust anything I wrote just because it was me who wrote it. Same for other saints.

      We're human, we err, and sometimes we just misread questions ;-)

      (I assume that was just a polite and humorous way to tell me that I'm wrong, but if not I'd like to correct that view)

Re: Perl 5.10 Named Capture Bug?
by moritz (Cardinal) on Jul 08, 2008 at 20:42 UTC
    This is a feature, because it's analog to the case of positional captures.

    Perl 6 introduces properly nested match objects.

    Sorry, I misread the question. I thought you were expecting nested hashes in %+. And no, It's a bug, not a feature. Sorry for the noise.

      No worries moritz. Thank you both for the fast and helpful replies. Cheers!
Re: Perl 5.10 Named Capture Bug?
by maletin (Sexton) on Jul 09, 2008 at 11:57 UTC
    I think, you want to use %- and not %+. see also "perldoc perlre" and "perldoc perlvar".