为了重用一些已编写的代码库,我需要将hash(%bar)伪装成另一个hash(%foo),以便在绑定后%bar可以看到所做的每个更改.%foo
使用匿名哈希构造,很容易:只需将ref复制bar到foo ($foo = $bar;)的ref中.
如何"不使用"匿名哈希(因为我必须重用的代码不使用匿名哈希构造)?这是唯一可能的吗?
谢谢.
use strict;
use Data::Dumper;
my %foo = ();
my %bar = ();
%foo = %bar; # This won't work as it copies %bar into %foo as at the current time.
$bar{A}->{st} = 'a';
$bar{A}->{qt} = 'a';
$bar{B}->{st} = 'b';
$bar{B}->{qt} = 'b';
# $foo{A}->{st} doesn't exist
Run Code Online (Sandbox Code Playgroud)
当然,匿名哈希构造本来就是一种祝福.
use strict;
use Data::Dumper;
my $foo = {};
my $bar = {};
$foo …Run Code Online (Sandbox Code Playgroud)