Welcome to the Monastery | |
PerlMonks |
Re: Hash values and constantsby ihb (Deacon) |
on Jan 12, 2003 at 15:28 UTC ( [id://226275]=note: print w/replies, xml ) | Need Help?? |
Good question.
First a quick note: Although %hash->{key} works you shouldn't do that. $hash{key} is the right way to use a hash. This is a known bug. Anyhow, the problem boils down to that $hash{KEY} is interpreted as $hash{'KEY'} and that a bareword to the left of => also is interpreted as a string. So what you have is: What you want to have is my %hash1 = (TESTVALUE() => 'OK'); or my %hash1 = (TESTVALUE, 'OK'); When getting the value with the constant you need to make it a non-bareword: $hash1{+TESTVALUE} or $hash1{TESTVALUE()} Update: I intentionally avoided mentioning & as a way of disambiguate the constant. This is because it will confuse and mislead people to think that you can do &foo; instead of foo(), which isn't the case. See perlfaq7. Hope I've helped, ihb
In Section
Seekers of Perl Wisdom
|
|