In Perl, which of these is the "better" style?
$hash{"string"} or $hash{string}?
In any case, are they functionality identical?
In Perl, which of these is the "better" style?
$hash{"string"} or $hash{string}?
In any case, are they functionality identical?
They're functionally identical, that is until your key has a space or some other non-alphanumeric character in it. Then you have to use the quoted approach.
I prefer to not quote and use names that contain alpha_num and the underscore. That lets me get away with not quoting most of the time.
The latter format, $hash{string}
, is more concise, but it only works for alphanumerics and underscores.
From perldata perldoc:
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 yes fundamentally identical. However beware of gotchas:
sub is_sub { 'Yep!' }
my %hash;
$hash{ is_sub } = 'Nope';
$hash{ is_sub() } = 'it is_sub!!';
say Dumper \%hash;
Will show:
$VAR1 = { 'is_sub' => 'Nope', 'Yep!' => 'it is_sub!!' };
My preference is for the bareword... but remember those () or a preceding + (see jrockway's comment and answer) if you're calling a sub ;-)
/I3az/
Out of pedantry, I always quote my string references in a hash. It serves to ensure that I'm not going to have a gotcha[1].
[1]I'm pretty conservative in my Perl coding.
The form without quotes is preferred by most, because it's more concise and uncluttered, as long as the hash key is limited to alphanumerics and the underscore. Also, most any editor with Perl syntax support knows how to highlight these as strings despite them not being within quotes.
It should be noted that you can get this behavior not only when using hash keys, but when defining hashes, passing arguments, or even when defining lists when you use =>
. It can help to visually distinguish your keys from your values in the former two cases. Examples:
# Hash construction
my %hash = (
key1 => "val1",
key2 => "val2"
);
# Subroutine arguments
some_function( arg1 => "val1", arg2 => $val2);
sub some_function { my %args = @_; }; # arguments are pulled in as a hash
# List construction (of course, not sure why you'd do this...)
my @list = (Foo => "bar");
print @list; # prints Foobar
All that said, I started out quoting everything and it's been a hard habit to break. It somehow feels "safer" to quote your hash keys, even after all these years.
Avoiding the quotes is more idiomatic.
Edit to add:
Quoting is not the solution to absolute readability. Consider the inconsistency here:
sub function() { 'OH HAI' }
my @list = ('foo', 'bar', function);
# ==> ('foo', 'bar', 'OH HAI')
my %hash;
$hash{'foo'} = 1;
$hash{'bar'} = 2;
$hash{function} = 3;
# ==> { foo => 1, bar => 2, function => 3 } (oops)
When you never quote strings, the "weird" things are visually different from the plain strings... and it is parsed correctly.
$hash{foo} = 1;
$hash{bar} = 2;
$hash{+function} = 3; # aha, this looks different... because it is
# ==> { foo => 1, bar => 2, 'OH HAI' => 3 }
(Stack Overflow's syntax highlighting fucks this up, so try to ignore it.)