| 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"
MC/DC0%0/1
our $CloneSelfMethod ||= "clone_self"
our $CloneSelfMethod
Truth table50%1/2
A
A= our $CloneSelfMethod
| |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | our $CloneInitMethod ||= 'clone_init'; | ||||||||||||||||||||||||||
|
Condition50%1/2
our $CloneInitMethod ||= "clone_init"
MC/DC0%0/1
our $CloneInitMethod ||= "clone_init"
our $CloneInitMethod
Truth table50%1/2
A
A= our $CloneInitMethod
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $depth and $depth-- < 1
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 47 | ▶ | return $CloneCache{ $source } if ( defined $CloneCache{ $source } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Non-reference values are copied shallowly | ||||||||||||||||||||||||||||
| 36 | 39 | ▶ | my $ref_type = ref $source or return $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 56 | 17 | $CloneCache{ $source } = $copy = {}; | |||||||||||||||||||||||||||
| 57 | 17 | ▶ | if ( my $tied = tied( %$source ) ) { tie %$copy, ref $tied } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 17 | ▶ | %$copy = map { ! ref($_) ? $_ : clone($_, $depth) } %$source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | @$copy = map { ! ref($_) ? $_ : clone($_, $depth) } @$source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | ▶ | } elsif ($ref_type eq 'REF' or $ref_type eq 'SCALAR') { | |||||||||||||||||||||||||||
|
Condition66%2/3
$ref_type eq "REF" or $ref_type eq "SCALAR"
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"
| |||||||||||||||||||||||||||||
| 64 | 10 | $CloneCache{ $source } = $copy = \( my $var = "" ); | |||||||||||||||||||||||||||
| 65 | 10 | ▶ | if ( my $tied = tied( $$source ) ) { tie $$copy, ref $tied } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | 9 | bless $copy, $class_name; | |||||||||||||||||||||||||||
| 76 | 9 | ▶ | $copy->$CloneInitMethod() if $copy->can($CloneInitMethod); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 36 | return $copy; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | 1; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||||||||||||