use strict; use warnings; use Storable qw(dclone); use Test::More; my %nested = ( a => { b=> 2, c=>3, d=>4}, e => { f => 6} ); my %copy1 = %nested; is_deeply( \%nested,\%copy1, "plain hash copies are equal" ); my %nested_with_refs = ( g => 7, h => \%nested, ); my %copy_with_refs = %nested_with_refs; is_deeply( \%nested_with_refs,\%copy_with_refs, "hash with references copies are equal" ); my $ref = \%nested_with_refs; my %ref_copy = %{$ref}; is_deeply( \%nested_with_refs,\%ref_copy, "reference and hash with references copies are equal" ); # original example: my %hash = (); $hash{a}{drinks}=1; $hash{b}{drinks}=2; my $p = \%hash; my %copy = %{ $p }; is_deeply( \%hash,\%copy , "also original example copies are equal" ); $copy{a}{drinks}=4; is_deeply( \%hash,\%copy , "also after changing \$copy{a}{drinks} copies are equal, because share the same ref" ); my $deep_copy_ref = dclone(\%hash); is_deeply( \%hash, $deep_copy_ref , "dclone copies are equal" ); $hash{a}{drinks} = 444; # this now fails is_deeply( \%hash, $deep_copy_ref , "THIS WILL FAIL: dclone copies are equal" ); # until.. $$deep_copy_ref{a}{drinks} = 444; is_deeply( \%hash, $deep_copy_ref , "dclone copies are equal again" ); done_testing();