tags:

views:

1831

answers:

13

Is it a good idea to quote keys when using a hash in Perl?

I am working on an extremely large legacy Perl code base and trying to adopt a lot of the best practices suggested by Damian Conway in Perl Best Practices. I know that best practices are always a touchy subject with programmers, but hopefully I can get some good answers on this one without starting a flame war. I also know that this is probably something that a lot of people wouldn't argue over due to it being a minor issue, but I'm trying to get a solid list of guidelines to follow as I work my way through this code base.

In the Perl Best Practices book by Damian Conway, there is this example which shows how alignment helps legibility of a section of code, but it doesn't mention (anywhere in the book that I can find) anything about quoting the hash keys.

$ident{ name   } = standardize_name($name);
$ident{ age    } = time - $birth_date;
$ident{ status } = 'active';

Wouldn't this be better written with quotes to emphasize that you are not using bare words?

$ident{ 'name'   } = standardize_name($name);
$ident{ 'age'    } = time - $birth_date;
$ident{ 'status' } = 'active';
+16  A: 

Without quotes is better. It's in {} so its obvious that you are not using barewords, plus it is both easier to read and type (two less symbols). But all of this depends on the programmer, of course.

ForYourOwnGood
Not using quotes has the added advantage that you'll never shoot yourself in the foot by accident when using double quotes and interpolating strings. `print "Hello $planet{"Earth"}\n"`; will cause you syntax errors, whereas `print "Hello $planet{Earth}\n";` will not.
pjf
+6  A: 

Quoteless hash keys received syntax-level attention from Larry Wall to make sure that there would be no reason for them to be other than best practice. Don't sweat the quotes.

(Incidentally, quotes on array keys are best practice in PHP, and there can be serious consequences to failing to use them, not to mention tons of E_WARNINGs. Okay in Perl != okay in PHP.)

chaos
Is there an interview or article with this reference to Larry Wall somewhere? I'd love to read it.
cowgod
Dunno. I was on some relevant mailing list while it was taking place.
chaos
+2  A: 

It is better with quotes because it allows you to use special characters not permitted in barewords. By using quotes I can use the special characters of my mother tongue in hash keys.

bandi
+4  A: 

Go with the quotes! They visually break up the syntax and more editors will support them in the syntax highlighting (hey, even stackoverflow is highlighting the quote version). I'd also argue that you'd notice typos quicker with editors checking that you ended your quote.

sammich
So you are suggesting quotes because you editor will show you when you forgot to end the quote?
innaM
Oddly enough, I think that is what he is saying. Either that, or he's saying that because strings are usually more prominently highlighted than vanilla code, it's easier to see typos in your hash keys?
Adam Bellaire
That's downright Orwellian logic.
Schwern
Orwellian? Really?Don't be lazy. Large amounts of Perl are hard enough to maintain without developers being lazy or "clever". Any practice you can do to help the code be easier to read and maintained by others is a step in the right direction.
sammich
+3  A: 

At least, quoting prevent syntax highlighting reserved words in not-so-perfect editors. Check out:

$i{keys} = $a;
$i{values} = [1,2];
...
Hynek -Pichi- Vychodil
+5  A: 

I don't think there's a best practice on this one. Personally I use them in hash keys like so:

$ident{'name'} = standardize_name($name);

but don't use them to the left of the arrow operator:

$ident = {name => standardize_name($name)};

Don't ask me why, it's just the way I do it :)

I think the most important thing you can do is to always, always, always:

use strict;
use warnings;

That way the compiler will catch any semantic errors for you, leaving you less likely to mistype something, whichever way you decide to go.

And the second most important thing is to be consistent.

Joe Casadonte
Yes, consistency is key. I'm trying to get a consistent style throughout this code base and this is one of the areas that varies from script to script. Fortunately, all the scripts do use strict, but most do not currently use warnings. This is being rectified as I go. :)
cowgod
+9  A: 

When specifying constant string hash keys, you should always use (single) quotes. E.g., $hash{'key'} This is the best choice because it obviates the need to think about this issue and results in consistent formatting. If you leave off the quotes sometimes, you have to remember to add them when your key contains internal hypens, spaces, or other special characters. You must use quotes in those cases, leading to inconsistent formatting (sometimes unquoted, sometimes quoted). Quoted keys are also more likely to be syntax-highlighted by your editor.

Here's an example where using the "quoted sometimes, not quoted other times" convention can get you into trouble:

$settings{unlink-devices} = 1; # I saved two characters!

That'll compile just fine under use strict, but won't quite do what you expect at runtime. Hash keys are strings. Strings should be quoted as appropriate for their content: single quotes for literal strings, double quotes to allow variable interpolation. Quote your hash keys. It's the safest convention and the simplest to understand and follow.

John Siracusa
This was obviously a subjective question, so I chose a subjective answer. After reading everyone's answer I am leaning to using the quotes always rather than only in cases where they are needed both for consistency and possible error reduction.
cowgod
A: 

I prefer to go without quotes, unless I want some string interpolation. And then I use double quotes. I liken it to literal numbers. Perl would really allow you to do the following:

$achoo['1']  = 'kleenex';
$achoo['14'] = 'hankies';

But nobody does that. And it doesn't help with clarity, simply because we add two more characters to type. Just like sometimes we specifically want slot #3 in an array, sometimes we want the PATH entry out of %ENV. Single-quoting it add no clarity as far as I'm concerned.

The way Perl parses code makes it impossible to use other types of "bare words" in a hash index.

Try

$myhash{shift}

and you're only going to get the item stored in the hash under the 'shift' key, you have to do this

$myhash{shift()}

in order to specify that you want the first argument to index your hash.

In addition, I use jEdit, the ONLY visual editor (that I've seen--besides emacs) that allows you total control over highlighting. So it's doubly clear to me. Anything looking like the former gets KEYWORD3 ($myhash) + SYMBOL ({) + LITERAL2 (shift) + SYMBOL (}) if there is a paranthesis before the closing curly it gets KEYWORD3 + SYMBOL + KEYWORD1 + SYMBOL (()}). Plus I'll likely format it like this as well:

$myhash{ shift() }
Axeman
Something like $myhash{shift} is exactly why you want to use quotes, IMHO. Sure, *you* know what you're doing, but how about the poor shmoe who has to maintain your code after you leave? He or she will look at that and thinK: Did the Axeman really mean the shift key or shift()?
Joe Casadonte
That's why 'shift' would probably never be an overt key, just incidental--referred to as $hash{$this_should_be_here}.
Axeman
A: 

You can precede the key with a - (minus character) too. From some of my code:

$args{-title} ||= "Intrig";

I use the single quote, double quote, and quoteless way too. All in the same program :-)

Mark Beckwith
You guys are killing me :-) Why the ding? It was a subjective question?
Mark Beckwith
And it's a subjective vote.
Schwern
The issue is that -title is != to title. They are two different keys.
jrockway
A: 

I've always used them without quotes but I would echo the use of strict and warnings as they pick out most of the common mistakes.

Jauder Ho
Except strict does nothing for hash keys, and a warning will likely fire far away from the mistaken hash read. This is one of the impetuses for inside-out objects and the ill-fated psuedohash (now restricted hashes).
Schwern
It might warn about use of undefined values though.
jplindstrom
Yes, when the hash *value* is used, usually far away from the point of the mistake and thus not terribly useful. This isn't to say don't use strict and warnings, just that they don't solve this problem.
Schwern
+1  A: 

I've wondered about this myself, especially when I found I've made some lapses:

 use constant CONSTANT => 'something';
 ...
 my %hash = ()
 $hash{CONSTANT}          = 'whoops!';  # Not what I intended
 $hash{word-with-hyphens} = 'whoops!';  # wrong again

What I tend to do now is to apply quotes universally on a per-hash basis if at least one of the literal keys needs them; and use parentheses with constants:

 $hash{CONSTANT()} = 'ugly, but what can you do?';
Adrian Pronk
+3  A: 

I go without quotes, just because it's less to type and read and worry about. The times when I have a key which won't be auto-quoted are few and far between so as not to be worth all the extra work and clutter. Perhaps my choice of hash keys have changed to fit my style, which is just as well. Avoid the edge cases entirely.

Sort of the same reason I use " by default. It's more common for me to plop a variable in the middle of a string then to use a character that I don't want interpolated. Which is to say, I've more often written 'Hello, my name is $name' than "You owe me $1000".

Schwern
I second the double quoted string as a good default.
jplindstrom
+1  A: 

I never single-quote hash keys. I know that {} basically works like quotes do, except in special cases (a +, and double-quotes). My editor knows this too, and gives me some color-based cues to make sure that I did what I intended.

Using single-quotes everywhere seems to me like a "defensive" practice perpetrated by people that don't know Perl. Save some keyboard wear and learn Perl :)

With the rant out of the way, the real reason I am posting this comment...the other comments seem to have missed the fact that + will "unquote" a bareword. That means you can write:

sub foo {
    $hash{+shift} = 42;
}

or:

use constant foo => 'OH HAI';
$hash{+foo} = 'I AM A LOLCAT';

So it's pretty clear that +shift means "call the shift function" and shift means "the string 'shift'".

I will also point out that cperl-mode highlights all of the various cases correctly. If it doesn't, ping me on IRC and I will fix it :)

(Oh, and one more thing. I do quote attribute names in Moose, as in has 'foo' => .... This is a habit I picked up from working with stevan, and although I think it looks nice... it is a bit inconsistent with the rest of my code. Maybe I will stop doing it soon.)

jrockway