so I’m trying to write a subroutine that takes a hash parameter and adds a couple key-value pairs to it (by reference). So far, I’ve got this:
addParams(\%params);
sub addParams
{
my(%params) = %{$_[0]}; #First argument (as a hash)
$params{"test"} = "testing";
}
But for some reason, It doesn’t seem to add the ‘test’ key. I am new to Perl, but isn’t this how you pass a hash by reference? Thanks beforehand.
You can use the hash-ref without de-referencing it:
EDIT:
To address your code’s issue, when you do:
You’re actually making a copy of what the ref points to with %{…}. You can see this via a broken down example (no function, same functionality):
Run:
Both hashes have the original ‘foo => foo’, but now each have their different bar/baz’s.