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:58
Subject:
[perl #54974] Strange map {} Behavior When Returning Two Values
Message ID:
rt-3.6.HEAD-337-1211994607-173.54974-14-0@perl.org
On Wed May 28 09:22:22 2008, david@kineticode.com wrote:
> When I run this code:
> 
> my @a = map { "--$_" => $_ } @ARGV;
> 
> I get this compile-time error:
> 
> Not enough arguments for map at /Users/david/bin/try line 6, near "}
> @ARGV"
> syntax error at /Users/david/bin/try line 6, near "} @ARGV"
> Execution of /Users/david/bin/try aborted due to compilation errors.

Because Perl thinks you are constructing an anonymous hash. If you
disambiguate, it figures it out:

    my @a = map {; "--$_" => $_ } @ARGV;

Note the semicolon.

(Pardon the double-submit, but I forgot to CC p5p on the first go.)

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