That's right, a regular hash key is just a string. Things that are not strings are forced into their string representation.
my $coderef = sub { my ($name, $name2) = @_; say "hello $name and $name2"; }; my %hash2 = ( $coderef => 1, ); print keys %hash2;
Tie ing is the usual way to change this behavior, but Hash :: MultiKey will not help you, it has a different purpose: as the name says, you can have several keys, but again only simple lines:
use Hash::MultiKey qw(); tie my %hash2, 'Hash::MultiKey'; $hash2{ [$coderef] } = 1; foreach my $key (keys %hash2) { say 'Ref of the key is: ' . ref($key); say 'Ref of the list elements produced by array-dereferencing the key are:'; say ref($_) for @{ $key };
Use Tie :: RefHash instead . (Criticism of code: prefer this syntax with an arrow -> to dereference coderef.)
use 5.010; use strict; use warnings FATAL => 'all'; use Tie::RefHash qw(); my $coderef = sub { my ($name, $name2) = @_; say "hello $name and $name2"; }; $coderef->(qw(bob sue)); my %hash = (sayhi => $coderef); $hash{sayhi}->(qw(bob sue)); tie my %hash2, 'Tie::RefHash'; %hash2 = ($coderef => 1); foreach my $key (keys %hash2) { say 'Ref of the key is: ' . ref($key);
daxim
source share