develooper Front page | perl.perl5.porters | Postings from February 2001

Re: map refuses quoted key in hash creation

Thread Previous
From:
Nicholas Clark
Date:
February 26, 2001 12:35
Subject:
Re: map refuses quoted key in hash creation
Message ID:
20010226203503.T23333@plum.flirble.org
On Mon, Feb 26, 2001 at 06:20:47PM +0100, H . Merijn Brand wrote:
> Why does map allow unquoted left side of => op in return pair, but not a quoted
> equivalent?

I think it's the syntax guesser in toke.c which needs to work out whether
{ is the start of a block or a hash reference constructor
(toke.c, starting

    case '{':
      leftbracket:
        s++;
        if (PL_lex_brackets > 100) {
)

> my %hash = (
>     ( map {  $_  => "" } qw(key1 key2) ),
>     ( map {  $_  => 0  } qw(key3 key4) ),
>     ( map { "$_" => "" } qw(key5 key6) ),
>     ( map { "$_" => 0  } qw(key7 key8) ),
>     );

It's thinking " is a hash constructor, and expecting a "," after "}" IIRC

Rewrite as +"$_" and it will work

write it as

map {
 "$_" => "" } qw(key5 key6

and it won't make a difference in this case (but that would fool the guesser
the other way - ie into making {} a block when you wanted that and it was
guessing hashref. And who said perl wasn't sensitive to whitespace?)

Nicholas Clark

Thread Previous


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