Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Perl 5.10 Named Capture Bug?

by delfarms (Novice)
on Jul 08, 2008 at 20:33 UTC ( #696327=perlquestion: print w/replies, xml ) Need Help??
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 ='ab') for k in m.groupdict(): print k, '=>', # 'A => ab' # 'B => b' print'A') # 'ab' print'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 CardinalNumber (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".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://696327]
Approved by moritz
Front-paged by friedo
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2016-12-03 22:10 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (60 votes). Check out past polls.