2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.11 2003/02/20 21:13:54 joko |
6 |
|
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
7 |
|
## |
8 |
## Revision 1.10 2003/02/20 20:48:00 joko |
## Revision 1.10 2003/02/20 20:48:00 joko |
9 |
## - refactored lots of code to Data::Code::Ref |
## - refactored lots of code to Data::Code::Ref |
10 |
## + alternative 'deep_copy' implementation |
## + alternative 'deep_copy' implementation |
444 |
# ACK's go to Andrew Pitonyak |
# ACK's go to Andrew Pitonyak |
445 |
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
446 |
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
|
sub deep_copy2 { |
|
|
|
|
|
# if not defined then return it |
|
|
return undef if $#_ < 0 || !defined( $_[0] ); |
|
|
|
|
|
# if not a reference then return the parameter |
|
|
return $_[0] if !ref( $_[0] ); |
|
|
my $obj = shift; |
|
|
if ( UNIVERSAL::isa( $obj, 'SCALAR' ) ) { |
|
|
my $temp = deep_copy2($$obj); |
|
|
return \$temp; |
|
|
} |
|
|
elsif ( UNIVERSAL::isa( $obj, 'HASH' ) ) { |
|
|
my $temp_hash = {}; |
|
|
foreach my $key ( keys %$obj ) { |
|
|
if ( !defined( $obj->{$key} ) || !ref( $obj->{$key} ) ) { |
|
|
$temp_hash->{$key} = $obj->{$key}; |
|
|
} |
|
|
else { |
|
|
$temp_hash->{$key} = deep_copy2( $obj->{$key} ); |
|
|
} |
|
|
} |
|
|
return $temp_hash; |
|
|
} |
|
|
elsif ( UNIVERSAL::isa( $obj, 'ARRAY' ) ) { |
|
|
my $temp_array = []; |
|
|
foreach my $array_val (@$obj) { |
|
|
if ( !defined($array_val) || !ref($array_val) ) { |
|
|
push ( @$temp_array, $array_val ); |
|
|
} |
|
|
else { |
|
|
push ( @$temp_array, deep_copy2($array_val) ); |
|
|
} |
|
|
} |
|
|
return $temp_array; |
|
|
} |
|
|
|
|
|
# ?? I am uncertain about this one |
|
|
elsif ( UNIVERSAL::isa( $obj, 'REF' ) ) { |
|
|
my $temp = deep_copy2($$obj); |
|
|
return \$temp; |
|
|
} |
|
|
|
|
|
# I guess that it is either CODE, GLOB or LVALUE |
|
|
else { |
|
|
return $obj; |
|
|
} |
|
|
} |
|
|
|
|
447 |
sub deep_copy { |
sub deep_copy { |
448 |
deep_copy2(@_); |
Pitonyak::deep_copy(@_); |
449 |
} |
} |
450 |
|
|
451 |
1; |
1; |