blib/lib/Clone/PP.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 91.1% 31 / 34 Conditioncond 70.0% 7 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 88.8% 96 / 108 CC 20 SCAR 27.9
CC
20
Cov
93%
clone
20.1
BEGIN
1.0
BEGIN
1.0
CRAP 16.3
↑ Summary
1 package Clone::PP;
2  
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = 1.09;
9  
10 7 our @EXPORT_OK = qw( clone );
11 7 ▶sub import { goto &Exporter::import } # lazy Exporter
Subroutine import: called CC=1 SCAR=0.0
12  
13 # These methods can be temporarily overridden to work with a given class.
14 7 ▶our $CloneSelfMethod ||= 'clone_self';
Condition50%1/2
our $CloneSelfMethod ||= "clone_self"
l!l
07
MC/DC0%0/1
our $CloneSelfMethod ||= "clone_self"
our $CloneSelfMethod
Truth table50%1/2
A
A= our $CloneSelfMethod
Aresult
11
00
15 7 ▶our $CloneInitMethod ||= 'clone_init';
Condition50%1/2
our $CloneInitMethod ||= "clone_init"
l!l
07
MC/DC0%0/1
our $CloneInitMethod ||= "clone_init"
our $CloneInitMethod
Truth table50%1/2
A
A= our $CloneInitMethod
Aresult
11
00
16  
17 # Used to detect looped networks and avoid infinite recursion.
18 7 our %CloneCache;
19  
20 # Generic cloning function
21 55 ▶sub clone {
Subroutine clone: called CC=20 SCAR=30.0
Pod undocumented
22 55 my $source = shift;
23  
24 55 ▶ return undef if not defined($source);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 3 52 55
25
26 # Optional depth limit: after a given number of levels, do shallow copy.
27 52 my $depth = shift;
28 52 ▶ return $source if ( defined $depth and $depth -- < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $depth and $depth-- < 1 5 47 52
Condition100%3/3
defined $depth and $depth-- < 1
!ll&&!rl&&r
3895
MC/DC100%2/2
defined $depth and $depth-- < 1
defined $depth $depth-- < 1
Truth table100%3/3
A and B
A= defined $depthB= $depth-- < 1
ABresult
0X0
100
111
29
30 # Maintain a shared cache during recursive calls, then clear it at the end.
31 47 ▶ local %CloneCache = ( undef => undef ) unless ( exists $CloneCache{undef} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Clone::PP::CloneCache{"undef"} 17 30 47
32
33 47 ▶ return $CloneCache{ $source } if ( defined $CloneCache{ $source } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $Clone::PP::CloneCache{$source} 8 39 47
34
35 # Non-reference values are copied shallowly
36 39 ▶ my $ref_type = ref $source or return $source;
Branch: 1 branch
BranchTrueFalseTotal
my $ref_type = ref $source or return $source 36 3 39
37
38 # Extract both the structure type and the class name of referent
39 36 my $class_name;
40 36 ▶ if ( "$source" =~ /^\Q$ref_type\E\=([A-Z]+)\(0x[0-9a-f]+\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$source" =~ /^\Q$ref_type\E\=([A-Z]+)\(0x[0-9a-f]+\)$/) 9 27 36
41 9 $class_name = $ref_type;
42 9 $ref_type = $1;
43 # Some objects would prefer to clone themselves; check for clone_self().
44 9 ▶ return $CloneCache{ $source } = $source->$CloneSelfMethod()
Branch: 1 branch
BranchTrueFalseTotal
if $source->can($Clone::PP::CloneSelfMethod) 0 9 9
45 if $source->can($CloneSelfMethod);
46 }
47
48 # To make a copy:
49 # - Prepare a reference to the same type of structure;
50 # - Store it in the cache, to avoid looping if it refers to itself;
51 # - Tie in to the same class as the original, if it was tied;
52 # - Assign a value to the reference by cloning each item in the original;
53
54 36 my $copy;
55 36 ▶ if ($ref_type eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_type eq 'HASH') { } 17 19 36
elsif ($ref_type eq 'ARRAY') { } 9 10 19
elsif ($ref_type eq 'REF' or $ref_type eq 'SCALAR') { } 10 0 10
56 17 $CloneCache{ $source } = $copy = {};
57 17 ▶ if ( my $tied = tied( %$source ) ) { tie %$copy, ref $tied }
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied %$source) 1 16 17
58 17 ▶ %$copy = map { ! ref($_) ? $_ : clone($_, $depth) } %$source;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 41 13 54
59 } elsif ($ref_type eq 'ARRAY') {
60 9 $CloneCache{ $source } = $copy = [];
61 9 ▶ if ( my $tied = tied( @$source ) ) { tie @$copy, ref $tied }
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied @$source) 1 8 9
62 9 ▶ @$copy = map { ! ref($_) ? $_ : clone($_, $depth) } @$source;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 10 13 23
63 ▶ } elsif ($ref_type eq 'REF' or $ref_type eq 'SCALAR') {
Condition66%2/3
$ref_type eq "REF" or $ref_type eq "SCALAR"
l!l&&r!l&&!r
370
MC/DC0%0/2
$ref_type eq "REF" or $ref_type eq "SCALAR"
$ref_type eq "REF" $ref_type eq "SCALAR"
Truth table66%2/3
A or B
A= $ref_type eq "REF"B= $ref_type eq "SCALAR"
ABresult
1X1
011
000
64 10 $CloneCache{ $source } = $copy = \( my $var = "" );
65 10 ▶ if ( my $tied = tied( $$source ) ) { tie $$copy, ref $tied }
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied $$source) 1 9 10
66 10 $$copy = clone($$source, $depth);
67 } else {
68 # Shallow copy anything else; this handles a reference to code, glob, regex
69 0 $CloneCache{ $source } = $copy = $source;
70 }
71
72 # - Bless it into the same class as the original, if it was blessed;
73 # - If it has a post-cloning initialization method, call it.
74 36 ▶ if ( $class_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_name) 9 27 36
75 9 bless $copy, $class_name;
76 9 ▶ $copy->$CloneInitMethod() if $copy->can($CloneInitMethod);
Branch: 1 branch
BranchTrueFalseTotal
if $copy->can($Clone::PP::CloneInitMethod) 0 9 9
77 }
78
79 36 return $copy;
80 }
81  
82 7 1;
83  
84 __END__
↑ Summary