# subroutine name: copy_struct # Input: reference to Perl structure (without loops!!!) # Returns: reference to copy of structure sub copy_struct { my ($struct) = @_; my $res; if (ref($struct) eq "SCALAR") { my $copy=$$struct; $res=\$copy; } elsif (ref($struct) eq "ARRAY") { my @copy=(); foreach (@{$struct}) { push(@copy,©_struct($_)) }; $res=\@copy; } elsif (ref($struct) eq "HASH") { my %copy=(); foreach (keys %{$struct}) { $copy{$_}=©_struct($struct->{$_}) }; $res=\%copy; } elsif (ref($struct) eq "REF") { my $copy=©_struct($$struct); $res=\$copy; } else { $res=$struct; } return $res; }