use strict; use warnings; use 5.012; use Parse::RecDescent; $::RD_ERRORS = 1; #Parser dies when it encounters an error $::RD_WARN = 1; #Enable warnings - warn on unused rules &c. $::RD_HINT = 1; #Give out hints to help fix problems. #$::RD_TRACE = 1; #Trace parsers' behaviour our %RESULTS; #Need to declare the variable, but my variables #won't be seen in another namespace. So declare #a global variable with our(). my $grammar = <<'END_OF_GRAMMAR'; #Start up action(executed in parser namespace): { use 5.012; #enable say() use Data::Dumper; } some_rule: name id { $main::RESULTS{names} = $item{name}; $main::RESULTS{phone_numbers} = $item{id}; } name: m{ \S+ }xms id: m{ \d+ }xms END_OF_GRAMMAR my $text = "Joe 10"; my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar!\n"; defined $parser->some_rule($text) or die "Text doesn't match"; use Data::Dumper; say Dumper(\%RESULTS); #Do something with %RESULTS --output:-- $VAR1 = { 'names' => 'Joe', 'phone_numbers' => '10' };