#!/usr/bin/perl -w use strict; use Data::Dumper; my @AoA = ( [1,2,3], [4,5,6], ); my @cloneAoA = map{[@$_]}@AoA; $AoA[0][1] = 99; $AoA[1]->[2] = 88; print Dumper \@AoA; print "the cloneAoA is still the same, but AoA changed!\n"; print Dumper \@cloneAoA; print "-------- now HoH ----------------\n"; my %HoH = ( 'a' => { 'x' => 1, 'y' => 2, }, 'b' => { 'xx' => 3, 'yy' => 4, }, ); my %cloneHoH; foreach my $key (keys (%HoH)) { $cloneHoH{$key} = { %{$HoH{$key}} }; } $HoH{'a'}{'x'} = 88; $HoH{'b'}{'yy'} = 99; print Dumper \%HoH; print "the clone is still the same, although %HoH changed!\n"; print Dumper \%cloneHoH; print "========= now HoHoH ==============\n"; my %HoHoH = ('x' => { 'ao' => { 'mx' => 1, 'my' => 2, 'mz' => 3, }, 'ap' => { 'nx' => 4, 'ny' => 5, }, }, 'y' => { 'bq' => { 'bx' => 6, 'by' => 7, }, }, ); #to clone the $HoHoH{'x'}{'ap'} sub-hash... my %xapClone = %{$HoHoH{'x'}{'ap'}}; $HoHoH{'x'}{'ap'}{'nx'} = 99; $HoHoH{'x'}{'ap'}{'ny'} = 88; print Dumper \%HoHoH; print "the %xapClone still has nx => 4 and ny => 5\n"; print Dumper \%xapClone; __END__ #output shown below.... in section #### $VAR1 = [ [ 1, 99, 3 ], [ 4, 5, 88 ] ]; the cloneAoA is still the same, but AoA changed! $VAR1 = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; -------- now HoH ---------------- $VAR1 = { 'a' => { 'y' => 2, 'x' => 88 }, 'b' => { 'yy' => 99, 'xx' => 3 } }; the clone is still the same, although %HoH changed! $VAR1 = { 'a' => { 'y' => 2, 'x' => 1 }, 'b' => { 'yy' => 4, 'xx' => 3 } }; ========= now HoHoH ============== $VAR1 = { 'y' => { 'bq' => { 'bx' => 6, 'by' => 7 } }, 'x' => { 'ao' => { 'mx' => 1, 'mz' => 3, 'my' => 2 }, 'ap' => { 'ny' => 88, 'nx' => 99 } } }; the %xapClone still has nx => 4 and ny => 5 $VAR1 = { 'ny' => 5, 'nx' => 4 };