【发布时间】:2019-09-24 20:16:48
【问题描述】:
我需要将变量、列表和哈希传递给子例程,并从中取回更改而不返回。
这段代码不能完成这项工作。
sub foo{
my $one = {$_[0]};
my @list = @{$_[1]};
my %hash = %{$_[2]};
print("\n inside foo: ${one}\n");
print("\n inside foo: $one @list $hash{'key'}\n");
$one = 2;
@list = (4,5,6);
my %hash2;
$hash2{'key'} = 'valueModified';
%hash = %hash2;
print("\n inside foo after Mod: $one\n");
print("\n inside foo after Mod: $one @list $hash{'key'}\n");
}
my $one = 1;
my @list = (1,2,3);
my %hash;
$hash{'key'} = 'value';
my @allLocalArgs = (\$one,\@list,\%hash);
foo(\@allLocalArgs);
print("\nafter foo modification: $one @list $hash{'key'}\n");
输出:
inside foo: HASH(0x234a240)
inside foo: HASH(0x234a240)
inside foo after Mod: 2
inside foo after Mod: 2 4 5 6 valueModified
after foo modification: 1 1 2 3 value
谢谢。
【问题讨论】:
-
这叫数组,不是列表。
标签: arrays list perl reference subroutine