Perlito5/Clone.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 57 CC 9 SCAR 44.0
CC
9
Cov
0%
_clone
90.0
clone
2.0
CRAP 81.2
1 package Perlito5::Clone;
2  
3 sub clone {
4 _clone( $_[0] );
5 }
6  
7 sub _clone {
8 my ($obj) = @_;
9  
10 return undef if !defined $obj;
11  
12 my $ref = ref($obj);
13 if ( !$ref ) {
14 return $obj;
15 }
16  
17 if ( $ref eq 'ARRAY' ) {
18 my @out;
19 for my $i ( 0 .. $#$obj ) {
20 push @out, _clone( $obj->[$i] );
21 }
22 return \@out;
23 }
24 elsif ( $ref eq 'HASH' ) {
25 my @out;
26 for my $i ( keys %$obj ) {
27 push @out, $i, _clone( $obj->{$i} );
28 }
29 return {@out};
30 }
31 elsif ( $ref eq 'SCALAR' || $ref eq 'REF' ) {
32 return \( _clone($$obj) );
33 }
34 elsif ( $ref eq 'CODE' ) {
35 return $ref;
36 }
37 elsif ( $ref eq 'GLOB' ) {
38 return $ref;
39 }
40  
41 my @out;
42 my $res;
43 local $@;
44 $res = eval {
45 for my $i ( 0 .. $#$obj ) {
46 push @out, _clone( $obj->[$i] ),;
47 }
48 bless( \@out, $ref );
49 };
50 return $res if $res;
51  
52 # assume it's a blessed HASH
53 for my $i ( keys %$obj ) {
54 push @out, $i, _clone( $obj->{$i} );
55 }
56 return bless( {@out}, $ref );
57 }
58  
59 1;
60