philcrow has asked for the wisdom of the Perl Monks concerning the following question:
I've started playing with the named capture and grammar features of 5.10. I like what I see, but I did find something that stumped me.
If I use a named capture I get what I expect:
But, when I try to DEFINE a grammar, the named capture only stores undef:#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $number_re = qr{ (?<int> \d+) }x; my $num = 15; if ( $num =~ /^$number_re$/ ) { warn "'$num' is valid\n"; warn Dumper( \%- ); } # produces: #'15' is valid #$VAR1 = { # 'int' => [ # '15' # ] # };
Am I missing something?#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $number_re = qr{ (?(DEFINE) (?<int> \d+ ) ) (?&int) }x; my $num = 15; if ( $num =~ /^$number_re$/ ) { warn "'$num' is valid\n"; warn Dumper( \%- ); } # produces: #'15' is valid #$VAR1 = { # 'int' => [ # undef # ] # };
(Perhaps my excitement at the new grammar features has raised my expectations too high. I was starting to hope for a parse tree.)
Phil
The Gantry Web Framework Book is now available.
Back to
Seekers of Perl Wisdom