You are getting incorrect results because when you make your copy of the hash using my %value = %$value_ref;
you are only getting the top-level keys. The second level, with the key 'xyz'
, is the one where value is actually stored so any changes to that reference are carried over. You are performing a shallow copy when you need a deep copy.
Luckily there is a CPAN module for that!
use strict;
use Storable qw(dclone);
sub test
{
my ($value_ref) = @_;
my %value = %{ dclone($value_ref) };
$value{'abc'}{'xyz'} = 1;
}
my %hash;
$hash{'abc'}{'xyz'} = 0;
test (\%hash);
print "$hash{'abc'}{'xyz'}\n"; # prints 0