Skip Menu |

This queue is for tickets about the Parse-RecDescent CPAN distribution.

Report information
The Basics
Id: 2818
Status: open
Priority: 0/
Queue: Parse-RecDescent

People
Owner: Nobody in particular
Requestors: tin.n.hoang [...] boeing.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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' } ];
The correct way to return something from an action is to assign to $return inside the action. The "return value" of the production (typically the value of the last statement) is used to determine if the production suceeds if it is defined. Modifying your grammar to include the following yields your desired behavior. assign: /\w+/ '=' /\w+/ ';' { $return { $item[1] => $item[3] } }
Subject: Re: [rt.cpan.org #2818] Action doesn't return an anonymous hash
Date: Mon, 23 Jan 2012 15:47:37 +1100
To: bug-Parse-RecDescent [...] rt.cpan.org
From: Damian Conway <damian [...] conway.org>
Show quoted text
> Modifying your grammar to include the following yields your desired > behavior. >    assign: /\w+/ '=' /\w+/ ';' { $return { $item[1] => $item[3] } }
Missing "=", I think: assign: /\w+/ '=' /\w+/ ';' { $return = { $item[1] => $item[3] } } BTW, perhaps that's an item for the ToDo list: add a check-and-warn for naked any 'return' in an action? Damian
Yes, missing an '=' sign. This is already in ToDo: Detect (and flag as an error) the use of return() is wrong in an action (e.g. "Did you mean '$return =' instead?" On Sun Jan 22 23:48:26 2012, damian@conway.org wrote: Show quoted text
> Missing "=", I think: > > BTW, perhaps that's an item for the ToDo list: add a check-and-warn for > naked any 'return' in an action?