in reply to Correct keys in hashes

Those examples are exactly equivalent. In general the only problem with these automatically quoted strings is that they need to be valid identifiers or integral numbers not starting with 0 (more or less matching /^([1-9]\d*)|([a-zA-Z_]\w*)$/).

update: as demonstrated by ikegami below, actually only valid identifiers are quoted: that means anything matching /^[a-zA-Z_]\w*$/ but not numbers.

As long as you use valid keys, and don't use version strings (/^v\d+$/) the quoted and unquoted keys are equivalent.

The version strings implementation makes $hash{v80} != $hash{'v80'} etc. in some versions of perl, so for portability these keys should always be quoted.

So, in short, if you don't want to think about the keys you use, quote them, but usually an intuitive key name can be used unquoted.

Replies are listed 'Best First'.
Re^2: Correct keys in hashes
by ikegami (Patriarch) on Aug 25, 2004 at 15:36 UTC
    automatically quoted strings [...] more or less matching /^([1-9]\d*)|([a-zA-Z_]\w*)$/

    No, it doesn't match numbers at all. If => autoquoted numbers, the key would be a bunch of 9s in the example below. Non-strings (numbers, references, etc) are converted to strings (although I think there is planned change to preserve the type of the key, maybe in perl6), but that's not the same as quoting. I think that means that if and only if it's a valid subroutine name, it'll get autoquoted.

    >perl -le " %a = (999999999999999999999999999999999999 => ''); print keys %a"; 1e+036 >perl -le " %a = (999999999999999999999999999999999999, ''); print keys %a"; 1e+036