Subject: | doc bug - assigning to $thisparser does not have the stated effect |
The pod has the following statement
---CUT----
The value of C<$thisparser> propagates down the subrules of a parse
but not back up. Hence, you can invoke subrules from another parser
for the scope of the current rule as follows:
rule: subrule1 subrule2
| { $thisparser = $::otherparser } <reject>
| subrule3 subrule4
| subrule5
The result is that the production calls "subrule1" and "subrule2" of
the current parser, and the remaining productions call the named subrules
from C<$::otherparser>. Note, however that "Bad Things" will happen if
C<::otherparser> isn't a blessed reference and/or doesn't have methods
with the same names as the required subrules!
---CUT---
Code inspection shows that the rules are called as fully qualified
subroutine names. E.g.
Parse::RecDescent::namespace000001::subrule3($thisparser)
This will not be dispatched based on $thisparser. So the above cannot
work. The attached script proves that it does not work.
My suggestion would be to simply ditch the verbiage in the pod.
Subject: | parsertest.pl |
#!/usr/bin/perl -w
use strict;
use warnings;
use Carp qw/confess/;
use Parse::RecDescent;
my $p1 = Parse::RecDescent->new(<<'GRAMMAR1');
<autostub>
TOP: rule /\z/
rule: subrule1 subrule2
| { $thisparser = $::otherparser } <reject>
| subrule3 subrule4
| subrule5
GRAMMAR1
defined($p1) or die "FAILED to create p1\n";
print "Main parser is in namespace " . $p1->{namespace} . "\n";
our $otherparser = Parse::RecDescent->new(<<'GRAMMAR2');
subrule3 : 'a'
subrule4 : 'b'
subrule5 : 'c'
GRAMMAR2
defined($otherparser) or die "FAILED to create otherparser\n";
print "otherparser is in namespace " . $otherparser->{namespace} . "\n";
$SIG{__DIE__} = sub { confess @_ };
if (defined($p1->TOP('a b'))) {
print "===== MATCHED ===\n";
} else {
print "===== NO MATCH ===\n";
}