in reply to Hash values and constants

As the others have mentioned, what you're tripping over is a DWIM (Do What I Mean) feature of Perl: under certain circumstances, some words are assumed to have quotes around them when used on the left of the double arrow (or "fat comma") and in hash lookup curlies. (Did I miss any cases? There's also special rules for package names, but they're a bit different.) You can tell Perl's parser that you really didn't mean a string there by adding the correct incantation:
#!/usr/local/bin/perl -wl use strict; use constant TESTVALUE => 1; my %hash1 = (TESTVALUE() => "OK"); my %hash2 = (123 => TESTVALUE); my $hashValue; print $hash1{1}; print $hash1{+TESTVALUE}; print $hash1{$hash2{123}};
As you see, you do not have to use the comma instead of the double arrow - as long as Perl knows you mean the left side to be an expression, not a string. The same is true for hash lookup curlies. Note that prepending a plus sign won't work for the fat comma, though appending parens will work for the hash lookup. It's a bit confusing at first to remember which rules apply where, but you'll get used to them quick. In practice I find the specific rules and exceptions chosen to be the most convenient ones.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Hash values and constants
by ihb (Deacon) on Jan 12, 2003 at 16:39 UTC
    Besides -foo I can't come up with more other cases of "autoquoting". (But there always seem to be yet one more case! :))

    ihb