develooper Front page | perl.perl5.porters | Postings from April 2010

Re: [perl #74108] using $hash{value} should emit a warning

Thread Previous
From:
Zefram
Date:
April 8, 2010 02:20
Subject:
Re: [perl #74108] using $hash{value} should emit a warning
Message ID:
20100408092025.GA17930@lake.fysh.org
David A. Madore wrote:
>When 'value' is a valid bareword, and '%hash' is a hash, using
>'$hash{value}' is interpreted as '$hash{"value"}' and not
>'$hash{(value)}' as one might expect.  E.g.:

This is very much intentional.  Use of fixed identifier-like strings
as hash keys is extremely common, and worthy of such shorthand.  So the
interior of $hash{...} is interpreted specially, just like the left-hand
side of =>, with a bareword being interpreted as a literal string rather
than anything else it might have meant.

As you've discovered, you can override to get standard expression parsing
by enclosing the bareword in parens.  A prefix +, as in $hash{+value},
also achieves this, and is idiomatic.  You can also postfix parens,
as in $hash{value()}, to force interpretation as a niladic function.

-zefram

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