Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Hash Syntax Question

by BarMeister (Beadle)
on Jul 25, 2007 at 20:05 UTC ( #628775=perlquestion: print w/replies, xml ) Need Help??

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:


instead of this:


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


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.


    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://628775]
Approved by Joost
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-01-28 20:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found