Hi all,
I'm quite new to Moose and am using it for a new project, some of which
involves parsing, for which I'm using Parse::RecDescent. I've got a
couple of questions about integrating Parse::RecDescent with Moose --
specifically, about using Moose to write methods for objects created
without Moose.
If a grammar uses the <autotree> directive, Parse::RecDescent will
automatically build a parse tree, and
each node in the tree is blessed into a class of the same name
as the rule itself. This makes it easy to build object-oriented
processors for the parse-trees that the grammar produces.
To walk and print the parse tree I've got code to which I'm now adding
object support using Moose.
1. Consider a rule in the grammar which has the following code to print
the rule's own name:
package fielddec;
use Moose;
sub pr
{
my $self = shift;
if ( exists $self->{'__RULE__'} )
{
printf( "pr: %s\n", $self->{'__RULE__'} );
}
}
no Moose;
1;
Since I haven't yet defined Moose obejcts for all the grammar rules, the
code that walks the parse tree uses a kludge to look up the name of the
rule in a list and only call the Moose-defined method if the rule is in
the list:
my $sr = ref($self);
if ( grep { $sr eq $_; } @mooseobjs )
{
$self->pr();
}
This works, but surely there must be a better way to inspect the object
to determine if it defines the pr() method. If the objects had been
created by Moose I'd just get the meta object and call get_method_list,
but I'm stuck since they were created by Parse::RecDescent. Thoughts?
2. Since printing applies to all the grammar rule, I changed the code to
create a base class which is then extended for the particular rule:
package AutoTreeNode;
use Moose;
sub pr
{
my $self = shift;
if ( exists $self->{'__RULE__'} )
{
printf( "pr: %s\n", $self->{'__RULE__'} );
}
}
no Moose;
1;
package fielddec;
use Moose;
extends 'AutoTreeNode';
no Moose;
1;
But this doesn't work:
Can't locate object method "pr" via package "fielddec" at
./test_ParseTree line 78, <$TFILE> line 1.
How can it be fixed?
I'm really excited to have Moose available to speed the development of
this project and I look forward to the list's help in getting over these
hurdles with Moose.
Thanks in advance.
Tod
--
Tod Hagan
Information Technologist
AIRMAP/Climate Change Research Center
Institute for the Study of Earth, Oceans, and Space
University of New Hampshire
Durham, NH 03824
Phone: 603-862-3116