Closed p5pRT closed 14 years ago
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?
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
The RT System itself - Status changed from 'new' to 'open'
@rgs - Status changed from 'open' to 'rejected'
* David A. Madore \perlbug\-followup@​perl\.org [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.
Regards\, -- Aristotle Pagaltzis // \<http://plasmasturm.org/>
Migrated from rt.perl.org#74108 (status was 'rejected')
Searchable as RT74108$