I understand that bare words can be used as strings in quoting operators (q//, qq//), hash keys, etc. I'm personally not too thrilled about this because I can't get over the mental expectation that a literal string should be quoted, as in C, C++. However, if I were to adopt mixed uses of quoted strings and bare words, I want to make sure that I don't accidentally shoot myself in the foot where bare words would not behave correctly at runtime.
Please exclude use cases where 'use strict' would catch them as errors at compile time. I always enable 'strict' mode, so I'm not concerned about these cases.
Below is a code illustration based on the answers and comments provided:
#!/usr/bin/perl
use strict;
use constant SIZE => "const_size";
sub size {
return "getsize";
}
my $href = {
size => 1,
getsize => 2,
const_size => "CONST_SIZE",
SIZE => "LARGE",
};
print "constant SIZE:", SIZE, "\n";
print "1. \$href->{size}:", $href->{size}, "\n";
print "1a. \$href->{size()}:", $href->{size()}, "\n";
print "2. \$href->{getsize}:", $href->{getsize}, "\n";
print "3. \$href->{SIZE}:", $href->{SIZE}, "\n";
print "3a. \$href->{(SIZE)}:", $href->{(SIZE)}, "\n";
Output:
$ ./bare_word.pl
constant SIZE:const_size
1. $href->{size}:1
1a. $href->{size()}:2
2. $href->{getsize}:2
3. $href->{SIZE}:LARGE
3a. $href->{(SIZE)}:CONST_SIZE
It appears that with respect to hash keys, bare words behave as expected in all cases. To override the behavior, we'd need to explicitly disambiguate.