As described in perldoc perldata:
...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'}++
.
In general, if you have a hash key with a character outside the [A-Za-z0-9_]
range, use quotes (either single or double) inside the braces. As with normal strings, contents in double quotes will be parsed for any contained variables, while single quoted strings are taken literally:
use strict; use warnings;
use Data::Dumper;
my $x = 1;
my %hash = (
bare_string => 'hi there',
"not a bare string" => 'yup',
);
$hash{'$x'} = 'foo';
$hash{"$x"} = 'bar';
print Dumper(\%hash);
prints:
$VAR1 = {
'bare_string' => 'hi there',
'not a bare string' => 'yup',
'$x' => 'foo'
'1' => 'bar',
};