Subject: | Action doesn't return an anonymous hash |
Requested info:
Parse-RecDescent-1.94
perl v5.8.0 built for PA-RISC1
OS => HP-UX B.10.20
------------------
Example code:
#! /bin/perl
use strict;
use Parse::RecDescent;
use Data::Dumper;
my $grammar = q(
file: assign(s)
assign: /\w+/ '=' /\w+/ ';' { { $item[1] => $item[3] } }
);
my $parser = Parse::Descent->new($grammar);
undef $/;
my $file = <DATA>;
my $data = $parser->file($file);
print Dumper($data);
__DATA__
access=deny;
index=yes;
<<<<< end of script >>>>>>
The lexical $data above should be an anonymous array of anonymous hashes. But the output shows that $data is just an anonymous array. Here's the output:
$VAR1 = [
'deny',
'yes'
];
I was expecting the output to be:
$VAR1 = [
{
'access' => 'deny'
},
{
'index' => 'yes'
}
];
Apparently, the parser thinks that '{ $item[1] => $item[3] }' is a block statement instead of an anonymous hash constructor. Since $item[3] is the last statement in the block statement, the value of $item[3] gets returned as the result of the production.
If I replace '{ $item[1] => $item[3] }' with '{ foo => $item[3] }' where the string 'foo' replaces $item[1], I correctly get the array of hashes. Here's the output:
$VAR1 = [
{
'foo' => 'deny'
},
{
'foo' => 'yes'
}
];