Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl 5.10 Named Capture Bug?

by delfarms (Novice)
on Jul 08, 2008 at 20:33 UTC ( [id://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 = 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".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://696327]
Approved by moritz
Front-paged by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-29 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found