dav*_*ave 5 perl hash reference dereference
如何在特定哈希键中创建对值的引用.我尝试了以下但是$$ foo是空的.任何帮助深表感谢.
$hash->{1} = "one";
$hash->{2} = "two";
$hash->{3} = "three";
$foo = \${$hash->{1}};
$hash->{1} = "ONE";
#I want "MONEY: ONE";
print "MONEY: $$foo\n";
Run Code Online (Sandbox Code Playgroud)
use strict;
use warnings;
my $hash;
$hash->{1} = "one";
$hash->{2} = "two";
$hash->{3} = "three";
my $foo = \$hash->{1};
$hash->{1} = "ONE";
print "MONEY: $$foo\n";
Run Code Online (Sandbox Code Playgroud)
打开严格和警告,你会得到一些关于出了什么问题的线索.
use strict;
use warnings;
my $hash = { a => 1, b => 2, c => 3 };
my $a = \$$hash{a};
my $b = \$hash->{b};
print "$$a $$b\n";
Run Code Online (Sandbox Code Playgroud)
一般来说,如果你想用切片或参考refs做事,你必须使用旧样式,堆积的sigil语法来获得你想要的.如果您不记得堆积的sigil语法详细信息,您可以找到方便的参考快速参考.
更新
正如murugaperumal指出的那样,你可以做到my $foo = \$hash->{a};
我可以发誓我尝试了它并且它不起作用(令我惊讶).我会把它归结为疲劳让我变得更加愚蠢.