perlquestion
philcrow
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.
<p>
If I use a named capture I get what I expect:
<code>
#!/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'
# ]
# };
</code>
But, when I try to DEFINE a grammar, the named capture only stores undef:
<code>
#!/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
# ]
# };
</code>
Am I missing something?
<p>
(Perhaps my excitement at the new grammar features has raised my expectations too high. I was starting to hope for a parse tree.)
<p>
Phil
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-480254">
The <a href='http://stores.lulu.com/philcrow'>Gantry Web Framework Book</a> is now available.
</div></div>