perl: added hash entry in a subroutine is lost
- by hansi
Why is the hash empty on the second call of printHash?
my %hash = ();
addToHash(\%hash);
printHash(\%hash);
sub addToHash {
my %hash = %{$_[0]};
$hash{"test"} = "test";
printHash(\%hash);
}
sub printHash {
print "printHash: \n";
my %hash = %{$_[0]};
foreach my $key (keys %hash) {
print "key: $key, value: $hash{$key}\n";
}
}
Output:
printHash:
key: test, value: test
printHash: