February 06, 2007

keys of anonymous hashref: perl bug?

$ perl -we 'print keys %{{}}'
Unmatched right curly bracket at -e line 1, at end of line
syntax error at -e line 1, near "%{{}}"
Execution of -e aborted due to compilation errors.

Posted by Alan at February 6, 2007 10:23 PM
Comments

Oddly,

perl -we 'print keys %{ { } }'

Exits cleanly with no output & no warnings. WTF?

Posted by: brennen at February 10, 2007 11:16 AM

...which is the correct behavior. Apparently in the problem case %{{} parses as "the hash named {{}" and the trailing brace is unmatched.

Posted by: Alan at February 16, 2007 03:55 PM

perl -we 'print keys %{+{}}'

is also apparently sufficiently unambiguous. I feel like this lives in toke.c, but after about 15 minutes of looking I'm confident I don't know what the hell is going on there.

Posted by: brennen at February 17, 2007 11:53 AM

You see the {+code} trick a lot, like:

sub foo {
my %args = %{+shift};
...
}

What a hack though.

Posted by: alan at February 18, 2007 02:34 PM
Post a comment












Remember personal info?