braden has asked for the wisdom of the Perl Monks concerning the following question:

Using Parse::RecDescent, how would one go about using autoactions with a precompiled grammar? I think I need to set RD_AUTOACTION before runnng the module to compile the grammar; but I'm not sure how to do that.

Replies are listed 'Best First'.
Re: Parse::RecDescent: autoactions and precompiled grammars
by philcrow (Priest) on Apr 10, 2006 at 14:33 UTC
    Thank you for pointing out that I can precompile the grammar into a module. This gave me a nearly four fold speed increase.

    To answer your question... I'm assuming you are using the command line approach to precompilation suggested in the perldoc:

    perl -MParse::RecDescent - grammar Yet::Another::Grammar
    If I read the docs correctly, you could write a little script instead. Then you can control all of the flag variables:
    use Parse::RecDescent; $::RD_AUTOACTION = ...; Parse::RecDescent->Precompile( $grammar_string, 'Your::Namespace::GrammarModule' );
    Phil
      Just to confirm (since the docs only mention this working with new and the parent wasn't confident in his answer), here's a working example of $::RD_AUTOACTION being set when Precompile is called:
      use Data::Dumper qw( Dumper ); use Parse::RecDescent (); { local $::RD_AUTOACTION = q { [@item] }; Parse::RecDescent->Precompile( 'parse: /a/ /b/ /c/', 'Grammar' ) or die("Bad grammar\n"); } { # Normally, this would be in another file. require Grammar; my $parser = Grammar->new(); print(Dumper($parser->parse('a b c'))); }

      outputs

      $VAR1 = [ 'parse', 'a', 'b', 'c' ];

      If you comment out autoaction, the output is

      $VAR1 = 'c';

      Yes, I was using the command line approach.

      Thanks!