Is there any way to use a constant as a hash key?
For example:
use constant X => 1;
my %x = (X => \'X\');
The above code will cr
Another option is to not use the use constant pragma and flip to Readonly as per recommendations in the Perl Best Practices by Damian Conway.
I switched a while back after realizing that constant hash ref's are just a constant reference to the hash, but don't do anything about the data inside the hash.
The readonly syntax creates "normal looking" variables, but will actually enforce the constantness or readonlyness. You can use it just like you would any other variable as a key.
use Readonly;
Readonly my $CONSTANT => 'Some value';
$hash{$CONSTANT} = 1;
Your problem is that => is a magic comma that automatically quotes the word in front of it. So what you wrote is equivalent to ('X', 'X').
The simplest way is to just use a comma:
my %x = (X, 'X');
Or, you can add various punctuation so that you no longer have a simple word in front of the =>:
my %x = ( X() => 'X' );
my %x = ( &X => 'X' );
Use $hash{CONSTANT()}
or $hash{+CONSTANT}
to prevent the bareword quoting mechanism from kicking in.
From: http://perldoc.perl.org/constant.html