in reply to Using Parse::RecDescent in an Object
Yuk. Just return the extracted data from your parse rule. What follows is a simpler example.
To build your parser module:
#!/usr/bin/env perl # make_grammar.pl use strict; use warnings; use Parse::RecDescent qw( ); my $grammar = <<'__END_OF_GRAMMAR__'; { use strict; use warnings; } parse : list /\Z/ { $item[1] } list : '(' term(s) ')' { $item[2] } term : /[a-z]+/ | list __END_OF_GRAMMAR__ Parse::RecDescent->PreCompile($grammar, 'NestedListParser') or die("Bad Grammar\n");
To load the file, you'd do something like:
use NestedListParser qw( ); sub load { my $self = shift; my $qfn = shift; open(my $fh, '<', $qfn) or croak("Couldn't open file \"$qfn\": $!"); my $file; { local $/; $file = <$fh>; } my $parser = NestedListParser->new(); $self->{list} = $parser->parse($file) or die("Bad data\n"); }
Updated: Changed to a grammar that better illustrates the solution.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using Parse::RecDescent in an Object
by ~~David~~ (Hermit) on Jun 10, 2010 at 18:47 UTC |