http://www.perlmonks.org?node_id=628775

BarMeister has asked for the wisdom of the Perl Monks concerning the following question:

Is there any difference between accessing a hash element like this:

$hash{key}

instead of this:

$hash{'key'}

I am not asking about style...just about whether I could ever expect odd behavior from using the first one.

Thanks,
Kenny

Replies are listed 'Best First'.
Re: Hash Syntax Question
by FunkyMonk (Chancellor) on Jul 25, 2007 at 20:11 UTC
    Yes, there's a difference. $hash{OneWord} is OK, but $hash{Two Words} is a syntax error.

    From perldata (emphasis mine):

    In fact, an identifier within such curlies is forced to be a string, as is any simple identifier within a hash subscript. Neither need quoting. Our earlier example, $days{'Feb'} can be written as $days{Feb} and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression. This means for example that $version{2.0}++ is equivalent to $version{2}++ , not to $version{'2.0'}++
    So, if it looks like an identifier, you don't need to quote it.
      also be aware of the following little gotcha:

      perl -wMstrict -e "my %h; $h{shift} = 'foo'; print %h" bar shiftfoo perl -wMstrict -e "my %h; $h{shift()} = 'foo'; print %h" bar barfoo
      ok thanks that was very helpful :-)
Re: Hash Syntax Question
by philcrow (Priest) on Jul 25, 2007 at 20:08 UTC
    The first one is fine, unless there are funny characters (like spaces or other punctuation) in the key name.

    Phil

    The Gantry Web Framework Book is now available.
Re: Hash Syntax Question
by Fletch (Bishop) on Jul 25, 2007 at 20:15 UTC

    The first contravenes PBP 45 ("Don't use barewords."), but behavior-wise as long as it's a plain bareword there should be no difference.

    Stylistically though it'll get you disapproving glares from the purists (granted I still violate 45 myself out of old habits; working with Ruby is slowly changing that (although too much Rails makes me tend to try $hash{ :key } instead . . . )).

Re: Hash Syntax Question
by mjscott2702 (Pilgrim) on Jul 25, 2007 at 21:00 UTC
    In this context, is there any difference between using single or double quotes, apart from the fact that single quotes will prevent variable interpolation?
    Maybe I've been using Java too much lately, but now I am getting paranoid about the number of "objects" I create ...
      No difference except for the one you mentioned. single and double-quoted strings both result in the same object except for the variable interpolation and related character escape rules.

      In theory, if you don't need interpolation, using single quotes could compile a tiny bit faster, but a) perl compiles blazingly fast compared to most languages, and b) the time needed to analyze a quoted string is probably dwarfed by the time needed to load the perl interpreter and the rest of the code. Don't worry about it.

      Also, using double-quotes makes it slightly easier to insert variables into the string later, which is why i generally prefer to use double-quoted strings for all literals that don't contain a lot of "meta" characters.

        And, if there's nothing in the string to interpolate, perl changes the double quotes to single quotes:

        zippy:~/scripts$ deparse -e '$h{q[one]} = 1; $h{qq[two]} = 2; $x = q[t +hr]; $h{qq[${x}ee]} = 3;' $h{'one'} = 1; $h{'two'} = 2; $x = 'thr'; $h{"${x}ee"} = 3; -e syntax OK

      Nope. As a rule, interpolation is the only difference between single and double quotes in Perl.