develooper Front page | perl.recdescent | Postings from January 2012

Re: How to access a variable from the grammar code

Thread Previous
From:
Damian Conway
Date:
January 10, 2012 15:35
Subject:
Re: How to access a variable from the grammar code
Message ID:
CAATtAp65CT8gJGiPAL9KDUjWMs09MdrHhvLg23bmbyAQHG5OAw@mail.gmail.com
On 11 January 2012 03:54, Yuri Shtil <yshtil@nvidia.com> asked:

> Is there an elegant way to pass a parameter to the Parse::RecDescent
> constructor and access it from the actions?

I'm afraid there isn't.

The best workaround is probably to make the shared resource an attribute
of the parser object (instead of using an external variable) and then
access it within the grammar via $thisparser. Like so:

my $grammar = q{
     startrule: a b c {$thisparser->{sc_obj}->method}
...
};

sub action {
    my $parser = Parse::RecDescent->new($grammar);
    $parser->{sc_obj} = SomeClass->new();
    $parser->startrule($sometext);
}


Damian

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About