在Perl中,我正在学习如何取消“子例程引用”。但我似乎不能使用子例程引用作为散列“键”。
在下面的示例代码中,
还有其他想法吗?
use strict;
#use diagnostics;
use Hash::MultiKey;
my $subref = \&hello;
#1:
&$subref('bob','sue'); #okay
#2:
my %hash;
$hash{'sayhi'}=$subref;
&{$hash{'sayhi'}}('bob','sue'); #okay
#3:
my %hash2;
tie %hash2, 'Hash::MultiKey';
$hash2{$subref}=1;
foreach my $key (keys %hash2) {
print "Ref type is: ". ref($key)."\n";
&{$key}('bob','sue'); # Not okay
}
sub hello {
my $name=shift;
my $name2=shift;
print "hello $name and $name2\n";
}这是返回的内容:
hello bob and sue
hello bob and sue
Ref type is: ARRAY
Not a CODE reference at d:\temp\test.pl line 21.发布于 2012-05-29 09:02:35
这是正确的,一个普通的散列键只是一个字符串。不是字符串的东西会被迫使用它们的字符串表示形式。
my $coderef = sub { my ($name, $name2) = @_; say "hello $name and $name2"; };
my %hash2 = ( $coderef => 1, );
print keys %hash2; # 'CODE(0x8d2280)'领带ing是修改该行为的常用方法,但是散列::多键对您没有帮助,它有一个不同的目的:正如名称所述,您可能有多个键,但同样只有简单的字符串:
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 }; # no output, i.e. simple strings
say 'List elements produced by array-dereferencing the key are:';
say $_ for @{ $key }; # 'CODE(0x8d27f0)'
}相反,使用领带::RefHash。(代码评论:更喜欢使用->箭头的语法来取消引用代码。)
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); # 'CODE'
$key->(qw(bob sue));
}发布于 2012-05-29 08:39:43
来自perlfaq4
如何使用引用作为哈希键? (由brian d foy和Ben Morrow提供) 散列键是字符串,因此不能真正使用引用作为键。当您尝试这样做时,perl将引用转换为它的字符串化形式(例如,散列(0xDEADBEEF ) )。在那里,您无法从字符串形式中获得引用,至少不需要自己做一些额外的工作。 请记住,即使引用的变量超出了作用域,散列中的条目仍然存在,Perl完全有可能随后在同一个地址分配一个不同的变量。这意味着一个新变量可能意外地与一个旧变量的值相关联。 如果您有Perl 5.10或更高版本,并且只想在引用中存储一个值,以便稍后查找,则可以使用核心哈希::Util::Field散列模块。如果您使用多个线程(这会导致在新地址重新分配所有变量,更改它们的字符串化)和垃圾--当引用的变量超出作用域时,这也将处理重命名键。 如果您实际上需要能够从每个散列条目获得一个真正的引用,您可以使用Tie::RefHash模块,它为您完成所需的工作。
所以看起来领带::RefHash会做你想做的事。但老实说,我不认为你想做的是一个特别好的主意。
发布于 2012-05-29 08:38:47
你为什么需要它?例如,如果需要将参数存储到散列中的函数,则可以使用HoH:
my %hash;
$hash{$subref} = { sub => $subref,
arg => [qw/bob sue/],
};
foreach my $key (keys %hash) {
print "$key: ref type is: " . ref($key) . "\n";
$hash{$key}{sub}->( @{ $hash{$key}{arg} } );
}但是,无论如何,你都可以选择一个更好的钥匙。
https://stackoverflow.com/questions/10795386
复制相似问题