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 my $text = <<'END_OF_TEXT'; 5:abcdefghi END_OF_TEXT my $grammar = <<'END_OF_GRAMMAR'; { use 5.012; use Data::Dumper; my $count_match; #****DECLARE A VARIABLE**** } line: section(s /:/) section: count { $count_match = $item{count} } #**SET THE VARIABLE** | word {say Dumper(\@item)} count: m{ \d+ }xms word: m| .{$count_match} |xms #***INTERPOLATE THE VARIABLE*** END_OF_GRAMMAR my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar!\n"; defined $parser->line($text) or die "Can't match text"; --output:-- $VAR1 = [ 'section', 'abcde' ];