in reply to Re: Hash keys not DWIMming
in thread Hash keys not DWIMming

the solution being to put the 512_x64 in single quotes

Yes, I know that works. It's just that I thought steps had been taken to avoid the need for quoting hash keys .... that's obviously not so and we're left with what looks like a dog's breakfast when it comes to deciding if a key needs to be quoted or not.

After spending a number of minutes looking at this, the best rule of practice I can come up with is "if the name of the key matches /^[0-9_\.]/ then quote it" - otherwise there's too many "ifs and buts" involved in trying to determine whether it will work without quotes.

Cheers,
Rob

Replies are listed 'Best First'.
Re^3: Hash keys not DWIMming
by 7stud (Deacon) on Oct 07, 2010 at 07:57 UTC
    It's just that I thought steps had been taken to avoid the need for quoting hash keys

    From "Learning Perl (5th)", p 95:

    ...the keys are always converted to strings. So, if you used the numeric expression 50/20 as the key, it would be turned into the three-character string "2.5"...

    And on p. 254, in a section titled "Unquoted Hash Keys":

    Perl offers many shortcuts that can help programmers. Here's a handy one: you may omit the quote marks on some hash keys.

    Of course, you can't omit the quote marks on just any key, since a hash key may be any arbitrary string. But keys are often simple. If the hash key is made up of nothing but letters, digits, and underscores without starting with a digit, you may be able to omit the quote marks. This kind of simple string without quote marks is called a bareword, since it stands alone without quotes.

    ...But beware: if there's anything inside the curly braces besides a bareword, Perl will interpret it as an expression.

Re^3: Hash keys not DWIMming
by trwww (Priest) on Oct 07, 2010 at 16:32 UTC

    It's just that I thought steps had been taken to avoid the need for quoting hash keys .... that's obviously not so and we're left with what looks like a dog's breakfast when it comes to deciding if a key needs to be quoted or not.

    The rule seems pretty straightforward to me. If the hash key is a valid identifier, it doesn't need quoted. Otherwise it does. Has always worked fine for my usage and always does what I want.