blib/lib/Hash/Merge.pm

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