blib/lib/Class/MakeMethods/Utility/Ref.pm

filter: Statementstmt 48.3% 30 / 62 Branchbran 19.5% 9 / 46 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 31.7% 47 / 148 CC 33 SCAR 58.9
CC
33
Cov
31%
_compare
600.0
_clone
12.8
import
2.1
CRAP 363.1
↑ Summary blib/lib/Class/MakeMethods/Utility/ArraySplicer.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Utility::Ref - Deep copying and comparison
4  
5 =head1 SYNOPSIS
6  
7 use Class::MakeMethods::Utility::Ref qw( ref_clone ref_compare );
8
9 $deep_copy = ref_clone( $original );
10 $positive_zero_or_negative = ref_compare( $item_a, $item_b );
11  
12 =head1 DESCRIPTION
13  
14 This module provides utility functions to copy and compare arbitrary references, including full traversal of nested data structures.
15  
16 =cut
17  
18 ########################################################################
19  
20 package Class::MakeMethods::Utility::Ref;
21  
22 1 $VERSION = 1.000;
23  
24 1 @EXPORT_OK = qw( ref_clone ref_compare );
25 1 ▶sub import { require Exporter and goto &Exporter::import } # lazy Exporter
Subroutine import: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
require Exporter and goto \&Exporter::import 1 0 1
26  
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 ######################################################################
30  
31 =head2 REFERENCE
32  
33 The following functions are provided:
34  
35 =head2 ref_clone()
36  
37 Make a recursive copy of a reference.
38  
39 =cut
40  
41 1 ▶use vars qw( %CopiedItems );
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # $deep_copy = ref_clone( $value_or_ref );
44 1 ▶sub ref_clone {
Subroutine ref_clone: called CC=1 SCAR=0.0
Pod documented
45 1 local %CopiedItems = ();
46 1 _clone( @_ );
47 }
48  
49 # $copy = _clone( $value_or_ref );
50 3 ▶sub _clone {
Subroutine _clone: called CC=9 SCAR=25.5
51 3 my $source = shift;
52
53 3 my $ref_type = ref $source;
54 3 ▶ return $source if (! $ref_type);
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_type 2 1 3
55
56 1 ▶ return $CopiedItems{ $source } if ( exists $CopiedItems{ $source } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $Class::MakeMethods::Utility::Ref::CopiedItems{$source} 0 1 1
57
58 1 my $class_name;
59 1 ▶ 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]+\)$/) 1 0 1
60 1 $class_name = $ref_type;
61 1 $ref_type = $1;
62 }
63
64 1 my $copy;
65 1 ▶ if ($ref_type eq 'SCALAR') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref_type eq 'SCALAR') { } 0 1 1
elsif ($ref_type eq 'REF') { } 0 1 1
elsif ($ref_type eq 'HASH') { } 1 0 1
elsif ($ref_type eq 'ARRAY') { } 0 0 0
66 0 $copy = \( $$source );
67 } elsif ($ref_type eq 'REF') {
68 0 $copy = \( _clone ($$source) );
69 } elsif ($ref_type eq 'HASH') {
70 1 $copy = { map { _clone ($_) } %$source };
71 } elsif ($ref_type eq 'ARRAY') {
72 0 $copy = [ map { _clone ($_) } @$source ];
73 } else {
74 0 $copy = $source;
75 }
76
77 1 ▶ bless $copy, $class_name if $class_name;
Branch: 1 branch
BranchTrueFalseTotal
if $class_name 1 0 1
78
79 1 $CopiedItems{ $source } = $copy;
80
81 1 return $copy;
82 }
83  
84 ######################################################################
85  
86 =head2 ref_compare()
87  
88 Attempt to recursively compare two references.
89  
90 If they are not the same, try to be consistent about returning a
91 positive or negative number so that it can be used for sorting.
92 The sort order is kinda arbitrary.
93  
94 =cut
95  
96 1 ▶use vars qw( %ComparedItems );
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 # $positive_zero_or_negative = ref_compare( $A, $B );
99 0 ▶sub ref_compare {
Subroutine ref_compare: not called CC=1 SCAR=6.9
Pod documented
100 0 local %ComparedItems = ();
101 0 _compare( @_ );
102 }
103  
104 # $positive_zero_or_negative = _compare( $A, $B );
105 0 ▶sub _compare {
Subroutine _compare: not called CC=24 SCAR=64.0
106 0 my($A, $B, $ignore_class) = @_;
107  
108 # If they're both simple scalars, use string comparison
109 0 ▶ return $A cmp $B unless ( ref($A) or ref($B) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $A or ref $B 0 0 0
Condition0%0/3
ref $A or ref $B
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $A or ref $B
ref $A ref $B
Truth table0%0/3
A or B
A= ref $AB= ref $B
ABresult
1X1
011
000
110
111 # If either one's not a reference, put that one first
112 0 ▶ return 1 unless ( ref($A) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $A 0 0 0
113 0 ▶ return - 1 unless ( ref($B) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $B 0 0 0
114
115 # Check to see if we've got two references to the same structure
116 0 ▶ return 0 if ("$A" eq "$B");
Branch: 1 branch
BranchTrueFalseTotal
if "$A" eq "$B" 0 0 0
117
118 # If we've already seen these items repeatedly, we may be running in circles
119 0 ▶ return undef if ($ComparedItems{ $A } ++ > 2 and $ComparedItems{ $B } ++ > 2);
Branch: 1 branch
BranchTrueFalseTotal
if $Class::MakeMethods::Utility::Ref::ComparedItems{$A}++ > 2 and $Class::MakeMethods::Utility::Ref::ComparedItems{$B}++ > 2 0 0 0
Condition0%0/3
$Class::MakeMethods::Utility::Ref::ComparedItems{$A}++ > 2 and $Class::MakeMethods::Utility::Ref::ComparedItems{$B}++ > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$Class::MakeMethods::Utility::Ref::ComparedItems{$A}++ > 2 and $Class::MakeMethods::Utility::Ref::ComparedItems{$B}++ > 2
$Class::MakeMethods::Utility::Ref::ComparedItems{$A}++ > 2 $Class::MakeMethods::Utility::Ref::ComparedItems{$B}++ > 2
Truth table0%0/3
A and B
A= $Class::MakeMethods::Utility::Ref::ComparedItems{$A}++ > 2B= $Class::MakeMethods::Utility::Ref::ComparedItems{$B}++ > 2
ABresult
0X0
100
111
120
121 # Check the ref values, which may be data types or class names
122 0 my $ref_A = ref($A);
123 0 my $ref_B = ref($B);
124 0 ▶ return $ref_A cmp $ref_B if ( ! $ignore_class and $ref_A ne $ref_B );
Branch: 1 branch
BranchTrueFalseTotal
if not $ignore_class and $ref_A ne $ref_B 0 0 0
Condition0%0/3
not $ignore_class and $ref_A ne $ref_B
!ll&&!rl&&r
000
MC/DC0%0/2
not $ignore_class and $ref_A ne $ref_B
not $ignore_class $ref_A ne $ref_B
Truth table0%0/3
not(A) and B
A= not $ignore_classB= $ref_A ne $ref_B
ABresult
0X0
100
111
125
126 # Extract underlying data types
127 0 ▶ my $type_A = ("$A" =~ /^\Q$ref_A\E\=([A-Z]+)\(0x[0-9a-f]+\)$/) ? $1 : $ref_A;
Branch: 1 branch
BranchTrueFalseTotal
"$A" =~ /^\Q$ref_A\E\=([A-Z]+)\(0x[0-9a-f]+\)$/ ? : 0 0 0
128 0 ▶ my $type_B = ("$B" =~ /^\Q$ref_B\E\=([A-Z]+)\(0x[0-9a-f]+\)$/) ? $1 : $ref_B;
Branch: 1 branch
BranchTrueFalseTotal
"$B" =~ /^\Q$ref_B\E\=([A-Z]+)\(0x[0-9a-f]+\)$/ ? : 0 0 0
129 0 ▶ return $type_A cmp $type_B if ( $type_A ne $type_B );
Branch: 1 branch
BranchTrueFalseTotal
if $type_A ne $type_B 0 0 0
130
131 0 ▶ if ($type_A eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type_A eq 'HASH') { } 0 0 0
elsif ($type_A eq 'ARRAY') { } 0 0 0
elsif ($type_A eq 'SCALAR' or $type_A eq 'REF') { } 0 0 0
132 0 my @kA = sort keys %$A;
133 0 my @kB = sort keys %$B;
134 0 ▶ return ( $#kA <=> $#kB ) if ( $#kA != $#kB );
Branch: 1 branch
BranchTrueFalseTotal
if $#kA != $#kB 0 0 0
135 0 foreach ( 0 .. $#kA ) {
136 return ( _compare($kA[$_], $kB[$_]) or
137 0 ▶ _compare($A->{$kA[$_]}, $B->{$kB[$_]}) or next );
Condition0%0/5
_compare($kA[$_], $kB[$_]) || _compare($$A{$kA[$_]}, $$B{$kB[$_]}) || (next)
l!l
00
_compare($kA[$_], $kB[$_]) or _compare($$A{$kA[$_]}, $$B{$kB[$_]})
l!l&&r!l&&!r
000
MC/DC0%0/2
_compare($kA[$_], $kB[$_]) || _compare($$A{$kA[$_]}, $$B{$kB[$_]}) || (next)
_compare($kA[$_], $kB[$_]) _compare($$A{$kA[$_]}, $$B{$kB[$_]})
Truth table0%0/3
A or B
A= _compare($kA[$_], $kB[$_])B= _compare($$A{$kA[$_]}, $$B{$kB[$_]})
ABresult
1X1
011
000
138 }
139 0 return 0;
140 } elsif ($type_A eq 'ARRAY') {
141 0 ▶ return ( $#$A <=> $#$B ) if ( $#$A != $#$B );
Branch: 1 branch
BranchTrueFalseTotal
if $A->$#* != $B->$#* 0 0 0
142 0 foreach ( 0 .. $#$A ) {
143 0 ▶ return ( _compare($A->[$_], $B->[$_]) or next );
Condition0%0/2
_compare($A->[$_], $B->[$_]) || (next)
l!l
00
MC/DC0%0/1
_compare($A->[$_], $B->[$_]) || (next)
_compare($A->[$_], $B->[$_])
Truth table0%0/2
A
A= _compare($A->[$_], $B->[$_])
Aresult
11
00
144 }
145 0 return 0;
146 ▶ } elsif ($type_A eq 'SCALAR' or $type_A eq 'REF') {
Condition0%0/3
$type_A eq "SCALAR" or $type_A eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type_A eq "SCALAR" or $type_A eq "REF"
$type_A eq "SCALAR" $type_A eq "REF"
Truth table0%0/3
A or B
A= $type_A eq "SCALAR"B= $type_A eq "REF"
ABresult
1X1
011
000
147 0 return _compare($$A, $$B);
148 } else {
149 0 return ("$A" cmp "$B")
150 }
151 }
152  
153 ########################################################################
154  
155 =head1 SEE ALSO
156  
157 See L<Class::MakeMethods> for general information about this distribution.
158  
159 See L<Ref> for the original version of the clone and compare functions used above.
160  
161 See L<Clone> (v0.09 on CPAN as of 2000-09-21) for a clone method with an XS implementation.
162  
163 The Perl6 RFP #67 proposes including clone functionality in the core.
164  
165 See L<Data::Compare> (v0.01 on CPAN as of 1999-04-24) for a Compare method which checks two references for similarity, but it does not provide positive/negative values for ordering purposes.
166  
167 =cut
168  
169 ######################################################################
170  
171 1 1;
↑ Summary blib/lib/Class/MakeMethods/Utility/ArraySplicer.pm »