程序中遇到一个问题,一个引用变量传入函数中,如果赋值一个新的引用,则外部引用没有改变,如果更改引用内容,则变量随之改变,做个记录,下次注意。
如下为示例:
sub test1{
my $x = shift;
$x = {left=>undef, right=>undef, key=>123, parent=>undef,};
}
sub test2{
my $x = shift;
$x->{key} = 123;
}
$t = {left=>undef, right=>undef, key=>undef, parent=>undef,};;
use Data::Dumper ;
test1($t);
print Dumper $t;
test2($t);
print Dumper $t;
$VAR1 = {
'left' => undef,
'parent' => undef,
'right' => undef,
'key' => undef
};
$VAR1 = {
'left' => undef,
'parent' => undef,
'right' => undef,
'key' => 123
};