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

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

Thread Previous | Thread Next
Aristotle Pagaltzis
April 8, 2010 14:22
Re: [perl #74108] using $hash{value} should emit a warning
Message ID:
* David A. Madore <> [2010-04-08 10:25]:
> 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.:
> #! /usr/local/bin/perl
> use strict;
> use warnings;
> use constant { value => "answer" };
> my %hash;
> $hash{value} = 42;
> This is a terrible cause of possible errors, because one can
> have thought safe to replace '"answer"' by 'value' everywhere
> in a program (or similarly have had '$answer' hold a constant
> value and thought to replace it by a constant).
> Would it be possible to emit a warning?

If you resolve it in the other direction, it might equally be
a terrible cause of errors, if the hash lookup was written first
and the constant is only added much later during maintenance (and
because constants are subs, this is package-based, which means
the constant can be in a completely different file).

If you include a warning for this case, then the same situation
would suddenly cause a hash lookup that has always done what it
was meant to to throw warnings. Even though the reason for those
warnings was the addition of a constant, ie. a line in the code
far away from the hash lookup.

I don’t always agree with Abigail when he uses this line, but
I’ll steal it for this case: this is a job for a lint-type
program such as Perl::Critic.

And it’s most certainly not a bug in Perl.

Aristotle Pagaltzis // <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About