use Parse::RecDescent qw( ); ... open(local *Parse::RecDescent::ERROR, '>', \my $error); my $p = Parse::RecDescent->new($grammar) or die "Invalid grammar"; my $output = $p->startrule($str) or die $ParseError; #### print("d"); BEGIN { print("b"); BEGIN { print("a"); } # inner BEGIN executed after this line is compiled print("c"); } # outer BEGIN executed after this line is compiled print("e"); # outermost scope executed after this line is compiled