blib/lib/Hash/Merge.pm

filter: Statementstmt 90.7% 167 / 184 Branchbran 68.0% 34 / 50 Conditioncond 50.0% 11 / 22 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 96.4% 54 / 56 Podpod 87.5% 7 / 8 totaltotal 82.5% 275 / 333 CC 39 SCAR 12.7
CC
39
Cov
83%
merge
8.1
_get_obj
7.5
_merge_hashes
6.7
CRAP 3.5
↑ Summary
1 package Hash::Merge;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Clone::Choose 0.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 our $CONTEXT;
12  
13 9 our $VERSION = '0.301';
14 9 our @EXPORT_OK = qw( merge _hashify _merge_hashes );
15 9 our %EXPORT_TAGS = ('custom' => [qw( _hashify _merge_hashes )]);
16  
17 sub _init
18 {
19 15 ▶ my $self = shift;
Subroutine _init: called CC=4 SCAR=13.9
20  
21 15 my $weak = $self;
22 15 weaken $weak;
23  
24 defined $self->{behaviors}
25 or $self->{behaviors} = {
26 'LEFT_PRECEDENT' => {
27 'SCALAR' => {
28 15 ▶ 'SCALAR' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 6 ▶ 'ARRAY' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 12 ▶ 'HASH' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 },
32 'ARRAY' => {
33 6 ▶ 'SCALAR' => sub { [@{$_[0]}, $_[1]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 6 ▶ 'ARRAY' => sub { [@{$_[0]}, @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 12 ▶ 'HASH' => sub { [@{$_[0]}, values %{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 },
37 'HASH' => {
38 12 ▶ 'SCALAR' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 12 ▶ 'ARRAY' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 33 ▶ 'HASH' => sub { $weak->_merge_hashes($_[0], $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 },
42 },
43  
44 'RIGHT_PRECEDENT' => {
45 'SCALAR' => {
46 12 ▶ 'SCALAR' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 6 ▶ 'ARRAY' => sub { [$_[0], @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 12 ▶ 'HASH' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 },
50 'ARRAY' => {
51 6 ▶ 'SCALAR' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 6 ▶ 'ARRAY' => sub { [@{$_[0]}, @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 12 ▶ 'HASH' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 },
55 'HASH' => {
56 12 ▶ 'SCALAR' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 12 ▶ 'ARRAY' => sub { [values %{$_[0]}, @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 30 ▶ 'HASH' => sub { $weak->_merge_hashes($_[0], $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 },
60 },
61  
62 'STORAGE_PRECEDENT' => {
63 'SCALAR' => {
64 12 ▶ 'SCALAR' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 6 ▶ 'ARRAY' => sub { [$_[0], @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 12 ▶ 'HASH' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 },
68 'ARRAY' => {
69 6 ▶ 'SCALAR' => sub { [@{$_[0]}, $_[1]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 6 ▶ 'ARRAY' => sub { [@{$_[0]}, @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 12 ▶ 'HASH' => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 },
73 'HASH' => {
74 12 ▶ 'SCALAR' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 12 ▶ 'ARRAY' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 30 ▶ 'HASH' => sub { $weak->_merge_hashes($_[0], $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
77 },
78 },
79  
80 'RETAINMENT_PRECEDENT' => {
81 'SCALAR' => {
82 12 ▶ 'SCALAR' => sub { [$_[0], $_[1]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 6 ▶ 'ARRAY' => sub { [$_[0], @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 12 ▶ 'HASH' => sub { $weak->_merge_hashes($weak->_hashify($_[0]), $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 },
86 'ARRAY' => {
87 6 ▶ 'SCALAR' => sub { [@{$_[0]}, $_[1]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
88 6 ▶ 'ARRAY' => sub { [@{$_[0]}, @{$_[1]}] },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 12 ▶ 'HASH' => sub { $weak->_merge_hashes($weak->_hashify($_[0]), $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 },
91 'HASH' => {
92 12 ▶ 'SCALAR' => sub { $weak->_merge_hashes($_[0], $weak->_hashify($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 12 ▶ 'ARRAY' => sub { $weak->_merge_hashes($_[0], $weak->_hashify($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 30 ▶ 'HASH' => sub { $weak->_merge_hashes($_[0], $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 },
96 },
97 15 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"behaviors"} or $self->{"behaviors"} = {"LEFT_PRECEDENT", {"SCALAR", {"SCALAR", sub { $_[0]; } , "ARRAY", sub { $_[0]; } , "HASH", sub { $_[0]; } }, "ARRAY", {"SCALAR", sub { [@{$_[0];}, $_[1]]; } , "ARRAY", sub { [@{$_[0];}, @{$_[1];}]; } , "HASH", sub { [@{$_[0];}, values %{$_[1];}]; } }, "HASH", {"SCALAR", sub { $_[0]; } , "ARRAY", sub { $_[0]; } , "HASH", sub { $weak->_merge_hashes($_[0], $_[1]); } }}, "RIGHT_PRECEDENT", {"SCALAR", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [@{$_[0];}, @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "HASH", {"SCALAR", sub { $_[1]; } , "ARRAY", sub { [values %{$_[0];}, @{$_[1];}]; } , "HASH", sub { $weak->_merge_hashes($_[0], $_[1]); } }}, "STORAGE_PRECEDENT", {"SCALAR", {"SCALAR", sub { $_[0]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { [@{$_[0];}, $_[1]]; } , "ARRAY", sub { [@{$_[0];}, @{$_[1];}]; } , "HASH", sub { $_[1]; } }, "HASH", {"SCALAR", sub { $_[0]; } , "ARRAY", sub { $_[0]; } , "HASH", sub { $weak->_merge_hashes($_[0], $_[1]); } }}, "RETAINMENT_PRECEDENT", {"SCALAR", {"SCALAR", sub { [$_[0], $_[1]]; } , "ARRAY", sub { [$_[0], @{$_[1];}]; } , "HASH", sub { $weak->_merge_hashes($weak->_hashify($_[0]), $_[1]); } }, "ARRAY", {"SCALAR", sub { [@{$_[0];}, $_[1]]; } , "ARRAY", sub { [@{$_[0];}, @{$_[1];}]; } , "HASH", sub { $weak->_merge_hashes($weak->_hashify($_[0]), $_[1]); } }, "HASH", {"SCALAR", sub { $weak->_merge_hashes($_[0], $weak->_hashify($_[1])); } , "ARRAY", sub { $weak->_merge_hashes($_[0], $weak->_hashify($_[1])); } , "HASH", sub { $weak->_merge_hashes($_[0], $_[1]); } }}} 15 0 15
98  
99 15 ▶ defined $self->{behavior} or $self->{behavior} = 'LEFT_PRECEDENT';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"behavior"} or $self->{"behavior"} = "LEFT_PRECEDENT" 15 0 15
100  
101 croak "Behavior '$self->{behavior}' does not exist"
102 15 ▶ if !exists $self->{behaviors}{$self->{behavior}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"behaviors"}{$self->{"behavior"}} 0 15 15
103  
104 15 $self->{matrix} = $self->{behaviors}{$self->{behavior}};
105 15 $self->{clone} = 1;
106 }
107  
108 sub new
109 {
110 15 ▶ my ($pkg, $beh) = @_;
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
111 15 ▶ $pkg = ref $pkg || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
0150
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
112  
113 15 ▶ my $instance = bless {($beh ? (behavior => $beh) : ())}, $pkg;
Branch: 1 branch
BranchTrueFalseTotal
$beh ? : 0 15 15
114 15 $instance->_init;
115  
116 15 return $instance;
117 }
118  
119 sub set_behavior
120 {
121 24 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine set_behavior: called CC=4 SCAR=14.9
Pod documented
122 24 my $value = shift;
123  
124 24 my @behaviors = grep { /^$value$/i } keys %{$self->{'behaviors'}};
125 24 ▶ if (scalar @behaviors == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @behaviors == 0) 0 24 24
126 {
127 0 carp 'Behavior must be one of : ' . join(', ', keys %{$self->{'behaviors'}});
128 0 return;
129 }
130 24 ▶ if (scalar @behaviors > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @behaviors > 1) 0 24 24
131 {
132 0 croak 'Behavior must be unique in uppercase letters! You specified: ' . join ', ', @behaviors;
133 }
134 24 ▶ if (scalar @behaviors == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @behaviors == 1) 24 0 24
135 {
136 24 $value = $behaviors[0];
137 }
138  
139 24 my $oldvalue = $self->{'behavior'};
140 24 $self->{'behavior'} = $value;
141 24 $self->{'matrix'} = $self->{'behaviors'}{$value};
142 24 return $oldvalue; # Use classic POSIX pattern for get/set: set returns previous value
143 }
144  
145 sub get_behavior
146 {
147 9 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine get_behavior: called CC=1 SCAR=0.0
Pod documented
148 9 return $self->{'behavior'};
149 }
150  
151 sub add_behavior_spec
152 {
153 12 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine add_behavior_spec: called CC=6 SCAR=18.4
Pod documented
154 12 my ($matrix, $name) = @_;
155 12 ▶ $name ||= 'user defined';
Condition50%1/2
$name ||= "user defined"
l!l
120
MC/DC0%0/1
$name ||= "user defined"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
156 12 ▶ if (exists $self->{'behaviors'}{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"behaviors"}{$name}) 6 6 12
157 {
158 6 carp "Behavior '$name' was already defined. Please take another name";
159 6 return;
160 }
161  
162 6 my @required = qw( SCALAR ARRAY HASH );
163  
164 6 foreach my $left (@required)
165 {
166 18 foreach my $right (@required)
167 {
168 54 ▶ if (!exists $matrix->{$left}->{$right})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $matrix->{$left}{$right}) 0 54 54
169 {
170 0 carp "Behavior does not specify action for '$left' merging with '$right'";
171 0 return;
172 }
173 }
174 }
175  
176 6 $self->{'behavior'} = $name;
177 6 $self->{'behaviors'}{$name} = $self->{'matrix'} = $matrix;
178 }
179  
180 9 ▶no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
181 9 *specify_behavior = \&add_behavior_spec;
182 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 sub get_behavior_spec
185 {
186 6 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine get_behavior_spec: called CC=3 SCAR=11.4
Pod documented
187 6 my ($name) = @_;
188 6 ▶ $name ||= 'user defined';
Condition50%1/2
$name ||= "user defined"
l!l
60
MC/DC0%0/1
$name ||= "user defined"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
189 6 ▶ exists $self->{'behaviors'}{$name} and return $self->{'behaviors'}{$name};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"behaviors"}{$name} and return $self->{"behaviors"}{$name} 6 0 6
190 return:
191 }
192  
193 sub set_clone_behavior
194 {
195 0 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine set_clone_behavior: not called CC=2 SCAR=17.9
Pod documented
196 0 my $oldvalue = $self->{'clone'};
197 0 ▶ $self->{'clone'} = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 0 0
198 0 return $oldvalue;
199 }
200  
201 sub get_clone_behavior
202 {
203 0 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine get_clone_behavior: not called CC=1 SCAR=6.9
Pod documented
204 0 return $self->{'clone'};
205 }
206  
207 sub merge
208 {
209 447 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine merge: called CC=8 SCAR=20.9
Pod documented
210  
211 447 my ($left, $right) = @_;
212  
213 # For the general use of this module, we want to create duplicates
214 # of all data that is merged. This behavior can be shut off, but
215 # can create havoc if references are used heavily.
216  
217 447 my $lefttype = ref($left);
218 447 ▶ $lefttype = "SCALAR" unless defined $lefttype and defined $self->{'matrix'}->{$lefttype};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lefttype and defined $self->{"matrix"}{$lefttype} 123 324 447
Condition66%2/3
defined $lefttype and defined $self->{"matrix"}{$lefttype}
!ll&&!rl&&r
0123324
MC/DC50%1/2
defined $lefttype and defined $self->{"matrix"}{$lefttype}
defined $lefttype defined $self->{"matrix"}{$lefttype}
Truth table66%2/3
A and B
A= defined $lefttypeB= defined $self->{"matrix"}{$lefttype}
ABresult
0X0
100
111
219  
220 447 my $righttype = ref($right);
221 447 ▶ $righttype = "SCALAR" unless defined $righttype and defined $self->{'matrix'}->{$righttype};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $righttype and defined $self->{"matrix"}{$righttype} 123 324 447
Condition66%2/3
defined $righttype and defined $self->{"matrix"}{$righttype}
!ll&&!rl&&r
0123324
MC/DC50%1/2
defined $righttype and defined $self->{"matrix"}{$righttype}
defined $righttype defined $self->{"matrix"}{$righttype}
Truth table66%2/3
A and B
A= defined $righttypeB= defined $self->{"matrix"}{$righttype}
ABresult
0X0
100
111
222  
223 447 ▶ if ($self->{'clone'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"clone"}) 447 0 447
224 {
225 447 ▶ $left = ref($left) ? clone($left) : $left;
Branch: 1 branch
BranchTrueFalseTotal
ref $left ? : 351 96 447
226 447 ▶ $right = ref($right) ? clone($right) : $right;
Branch: 1 branch
BranchTrueFalseTotal
ref $right ? : 351 96 447
227 }
228  
229 447 local $CONTEXT = $self;
230 447 return $self->{'matrix'}->{$lefttype}{$righttype}->($left, $right);
231 }
232  
233 # This does a straight merge of hashes, delegating the merge-specific
234 # work to 'merge'
235  
236 sub _merge_hashes
237 {
238 171 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine _merge_hashes: called CC=6 SCAR=19.0
239  
240 171 my ($left, $right) = (shift, shift);
241 171 ▶ if (ref $left ne 'HASH' || ref $right ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left ne "HASH" or ref $right ne "HASH") 0 171 171
Condition33%1/3
ref $left ne "HASH" or ref $right ne "HASH"
l!l&&r!l&&!r
00171
MC/DC0%0/2
ref $left ne "HASH" or ref $right ne "HASH"
ref $left ne "HASH" ref $right ne "HASH"
Truth table33%1/3
A or B
A= ref $left ne "HASH"B= ref $right ne "HASH"
ABresult
1X1
011
000
242 {
243 0 carp 'Arguments for _merge_hashes must be hash references';
244 0 return;
245 }
246  
247 171 my %newhash;
248 171 foreach my $key (keys %$left)
249 {
250 $newhash{$key} =
251 exists $right->{$key}
252 ? $self->merge($left->{$key}, $right->{$key})
253 543 ▶ : $left->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $right->{$key} ? : 411 132 543
254  
255 }
256  
257 171 foreach my $key (grep { !exists $left->{$_} } keys %$right)
258 {
259 132 $newhash{$key} = $right->{$key};
260 }
261  
262 171 return \%newhash;
263 }
264  
265 # Given a scalar or an array, creates a new hash where for each item in
266 # the passed scalar or array, the key is equal to the value. Returns
267 # this new hash
268  
269 sub _hashify
270 {
271 48 ▶ my $self = &_get_obj; # '&' + no args modifies current @_
Subroutine _hashify: called CC=5 SCAR=16.7
272 48 my $arg = shift;
273 48 ▶ if (ref $arg eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq "HASH") 0 48 48
274 {
275 0 carp 'Arguement for _hashify must not be a HASH ref';
276 0 return;
277 }
278  
279 48 my %newhash;
280 48 ▶ if (ref $arg eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 24 24 48
281 {
282 24 foreach my $item (@$arg)
283 {
284 48 my $suffix = 2;
285 48 my $name = $item;
286 48 ▶ while (exists $newhash{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $newhash{$name}) 0 48 48
287 {
288 0 $name = $item . $suffix++;
289 }
290 48 $newhash{$name} = $item;
291 }
292 }
293 else
294 {
295 24 $newhash{$arg} = $arg;
296 }
297 48 return \%newhash;
298 }
299  
300 9 my $_global;
301  
302 sub _get_obj
303 {
304 717 ▶ if (my $type = ref $_[0])
Subroutine _get_obj: called CC=7 SCAR=20.1
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = ref $_[0]) 699 18 717
305 {
306 return shift()
307 699 ▶ if $type eq __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if $type eq __PACKAGE__ or builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) 675 24 699
Condition50%3/6
$type eq __PACKAGE__ or builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
l!l&&r!l&&!r
675024
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2400
MC/DC0%0/3
$type eq __PACKAGE__ or builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
$type eq __PACKAGE__ builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table25%1/4
A or B and C
A= $type eq __PACKAGE__B= builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)
ABCresult
1XX1
0111
00X0
0100
308 || (blessed $_[0] && $_[0]->isa(__PACKAGE__));
309 }
310  
311 42 ▶ defined $CONTEXT and return $CONTEXT;
Branch: 1 branch
BranchTrueFalseTotal
defined $Hash::Merge::CONTEXT and return $Hash::Merge::CONTEXT 0 42 42
312 42 ▶ defined $_global or $_global = Hash::Merge->new;
Branch: 1 branch
BranchTrueFalseTotal
defined $_global or $_global = "Hash::Merge"->new 6 36 42
313 42 return $_global;
314 }
315  
316 9 1;
317  
318 __END__
↑ Summary