Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
LanX ... or is it "boying out" ?
[choroba]: RELEASE_TESTING=1 for Module::Starter based distributions
LanX ..."mistering out" ?
[talexb]: choroba AH! Thanks so much.
[choroba]: prove -bl xt in some others
[talexb]: OK, major issues with MANIFEST .. this'll take me a while.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2017-10-23 14:27 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (279 votes). Check out past polls.