develooper Front page | perl.perl5.porters | Postings from May 2008

[perl #54974] Strange map {} Behavior When Returning Two Values

Thread Previous | Thread Next
From:
A. Pagaltzis via RT
Date:
May 29, 2008 01:39
Subject:
[perl #54974] Strange map {} Behavior When Returning Two Values
Message ID:
rt-3.6.HEAD-11257-1212003417-99.54974-15-0@perl.org
On Wed May 28 10:55:59 2008, david@kineticode.com wrote:
> On May 28, 2008, at 10:08, A. Pagaltzis via RT wrote:
> > Because Perl thinks you are constructing an anonymous hash.
> > If you disambiguate, it figures it out:
> >
> >    my @a = map {; "--$_" => $_ } @ARGV;
> >
> > Note the semicolon.
> 
> Would it not also think that it was an anonymous hash when I
> used the  concatenation, as well?

It should, but apparently using a more complex expression makes
the parser reconsider its commitment to the hash constructor
interpretation of the curly braces.

Really this should be decided based on whether the closing brace
is followed by a comma or not, so in some sense this is certainly
a bug. But my understanding is that the syntactic ambiguity of
curly braces in Perl is difficult to handle in perl’s ultimately
yacc-derived (but heavily mutated) parser, which uses a limited
lookahead for curlies to work around the fact that this style of
parser strongly favours tokens with only a single meaning. In
practice, the heuristic works most of the time and the
disambiguation cues are easy to employ.

(You can equivalently disambiguate a hash constructor that gets
misparsed as a block by prepending a unary plus: `+{ ... }`.
Within such a block, statements are always a compile error.)

Someone else will have to tell you whether this is a WONTFIX tho.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About