#! perl -slw use strict; use Clone qw[clone]; use Data::Dumper; use Storable qw[dclone freeze]; use Benchmark qw[cmpthese]; sub deepcopy{ return $_[0] unless ref $_[0]; return [ map{ deepcopy($_) } @{$_[0]} ] if ref $_[0] eq 'ARRAY'; return { map{ deepcopy($_) } %{$_[0]} } if ref $_[0] eq 'HASH'; return \do{ ${$_[0]} }; } sub factorial { my ($f,$n) = (1,shift); $f *= $n-- while( $n ); $f; } sub multicheck { my $check = factorial($#_); my ($i, @ok) = (0, (0) x @_); while (@_) { my $first = shift; for my $next (@_) { $ok[$i] += ($first eq $next); } $i++; } my $count = do{ local $a; $a += $_ for @ok; $a; }; return $count == $check; } my @rs = \do{1..5}; my @rrs1 = \(@rs); my @rrs2 = \(@rs); my %h1 = do{ my $key='A'; map{ $key++ => $_} 1..5; }; my %hash = ( rs=> \@rs, rrs1=> \@rrs1, rrs2=> \@rrs2, h1=>\%h1 ); $hash{self} = $hash{rs}; my $DeepCopy = deepcopy( \%hash ); my $CloneCopy = clone( \%hash ); my $StorableCopy= dclone( \%hash ); #print Dumper [\%hash, $CloneCopy, $StorableCopy, $DeepCopy]; =pod Comment (moan) The following code should, according to my reading of the Data::Dumper pod, result in $DDumperCopy becoming a ref to a copy of %hash,, but it doesn't? :( my $DDumperCopy; $Data::Dumper::Purity = 1; $DDumperCopy = eval( Dumper(\%hash) ) and warn $@ if $@; print Dumper $DDumperCopy; =cut cmpthese( -1, { DeepCopy => '$DeepCopy = deepcopy( \%hash );', CloneCopy => '$CloneCopy = clone( \$hash );', StorableCopy=> '$StorableCopy = dclone( \%hash );', # DDumperCopy => '', }); my $strDeepCopy = freeze( $DeepCopy ); my $strCloneCopy = freeze( $CloneCopy ); my $strStorableCopy = freeze( $StorableCopy ); print "\nDifferences found!" unless multicheck( $strDeepCopy, $strCloneCopy, $strStorableCopy ); #print Dumper $DeepCopy, $CloneCopy, $StorableCopy # unless multicheck( $strDeepCopy, $strCloneCopy, $strStorableCopy );