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

quotes in hash keys

by kinaseman (Acolyte)
on May 21, 2006 at 13:48 UTC ( #550791=perlquestion: print w/replies, xml ) Need Help??
kinaseman has asked for the wisdom of the Perl Monks concerning the following question:

I remember reading, years ago, a recommendation that hash keys should always be quoted. The reason given was that future versions of Perl would not allow bare strings as hash keys. I have used quotation for keys religiously since, but many of the people who work with me do not. We therefore have both new and legacy code with bare hash keys. Is there any truth to the rumor about bare keys? Should I make this into a major style issue?

Note that this is not a question about whether one can use bare strings as keys, but about whether one should.

Replies are listed 'Best First'.
Re: quotes in hash keys
by adrianh (Chancellor) on May 21, 2006 at 14:07 UTC
    Is there any truth to the rumor about bare keys?

    For Perl 5 - no.

    Perl 6 will, of course, be different - but that shouldn't be an issue for people developing Perl 5.

    Note that this is not a question about whether one can use bare strings as keys, but about whether one should.

    I think you should personally.

      Yes, I think the comment was targeted at Perl 6, but five years ago it seemed that Perl 6 might be coming soon, and that we would want to use it.

      At this point, we have so much existing code that I'm not sure we have the resources to refactor all of it. Perl 6 is supposed to be back compatible with perl 5, but if it is successful I assume we will eventually move our major codes over. When we do this, not having to catch all the unquoted keys would be helpful. Supposedly there will be a converter. I hope someone is clever enough to write one that actually works; this is the achilles heel of TMTOWTDI perhaps.

      When will all this happen? three years? five years?

        Perl 6 is supposed to be back compatible with perl 5

        No it isn't. Perl 6 is substantially different from Perl 5. You will likely be able to run Perl 5 code under Perl 6 in some way, but new Perl 6 code is going to look very different from Perl 5 code. Worrying about Perl 6 conventions is you are writing Perl 5 is a waste of time :-)

        When will all this happen? three years? five years?

        When the people doing the work have the time available to make it happen :-)

Re: quotes in hash keys
by ioannis (Prior) on May 21, 2006 at 15:37 UTC
    You are at the skill level where only you know best what you should. But, remember that the terms inside the brackets are not always interpreted as string constants. For example, these are not equivalent:

    $name->{__PACKAGE__} $name->{__PACKAGE__.''} ;

    $name->{ 'PI()'} $name->{ PI() }

Re: quotes in hash keys
by BazB (Priest) on May 21, 2006 at 22:36 UTC

    As other posts have said, it doesn't look like barewords as hash keys are going away any time soon.
    As long as everyone knows when they must quote hash keys, then I wouldn't make a big deal out of it.

    If you can persuade everyone to use/agree on a set of coding standards and style, that's a good thing. It isn't always easy though.
    It my experience, things like quoting hash keys, bracketing/brace styles and the like aren't worth worrying about - concentrate on hassling/convincing your colleagues to build well engineered, robust, easily maintained code and let trival style issues slide.

    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: quotes in hash keys
by spiritway (Vicar) on May 22, 2006 at 01:12 UTC

    It sounds like you've already established a habit of using quotes. Since it might become an issue in the future, it does no harm to use them, and might possibly cause problems if you don't - so go ahead and use them. Chances are you're not going to be able to convince others to use quotes, if they're already in the habit of using barewords.

Re: quotes in hash keys
by Argel (Prior) on May 23, 2006 at 17:33 UTC
    Looking at Perl Best Practices I would have to say the answer is that you should not quote them (unless necessary). I do not think TheDamian addresses this specifically but all of his "good" examples contain unquoted keys. For example, see "Keys and Indices" on page 14.

    The closest he comes to explicitly covering this is under "Fat Commas" in the "Reserve => for pairs" section on pages 66 - 68. This quote particulalry stands out " [ The Fat Comma ] also removes the need to quote the key string . . . ." This is recommended because it reduces screen clutter and thus increases the readability of your code (see the "bad" example on page 67).

    So, if you like the guidlines set forth in Perl Best Practices then the answer is to only quote hash keys when necessary.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://550791]
Approved by wfsp
Front-paged by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2017-04-30 03:44 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (534 votes). Check out past polls.