package DeepCopy; require Exporter; @ISA=qw/Exporter/; @EXPORT=qw/copy/; sub copy { # copy $_[0] and return a freshly-reffed version. my ($out,$bless); my $orig = shift; my $type = ref $orig; return $orig unless $type; unless ($type =~ /SCALAR|REF|CODE|ARRAY|HASH|GLOB/) { $bless = $type; if (UNIVERSAL::isa($orig,'SCALAR')) { $type = 'SCALAR'; } elsif (UNIVERSAL::isa($orig,'REF')) { $type = 'REF'; } elsif (UNIVERSAL::isa($orig,'CODE')) { $type = 'CODE'; } elsif (UNIVERSAL::isa($orig,'ARRAY')) { $type = 'ARRAY'; } elsif (UNIVERSAL::isa($orig,'HASH')) { $type = 'HASH'; } elsif (UNIVERSAL::isa($orig,'GLOB')) { $type = 'GLOB'; } else { warn 'I am now deeply lost.'; return $orig; } } if ($type eq 'SCALAR' or $type eq 'REF' or $type eq 'CODE') { my $foo = $orig; $out = \$foo; } elsif ($type eq 'ARRAY') { push @$out, copy($_) for @$orig; } elsif ($type eq 'HASH') { $out->{$_} = copy($orig->{$_}) for keys %$orig; } elsif ($type eq 'GLOB') { warn 'GLOB copy unimplemented'; my $foo = $orig; $out = \$foo; } else { warn 'I am now deeply lost.'; } bless $out, $bless if $bless; return $out; } 1;