Perl 引用變量傳入函數中的行爲

程序中遇到一個問題,一個引用變量傳入函數中,如果賦值一個新的引用,則外部引用沒有改變,如果更改引用內容,則變量隨之改變,做個記錄,下次注意。


如下爲示例:

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
        };


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章