ModeMerge.pm

filter: Statementstmt 90.7% 195 / 215 Branchbran 81.7% 103 / 126 Conditioncond 82.0% 55 / 67 MC/DCmcdc 65.0% 26 / 40 Subroutinesub 86.3% 19 / 22 Podpod 90.0% 9 / 10 totaltotal 84.7% 407 / 480 CC 95 SCAR 28.8
CC
95
Cov
87%
_merge
41.2
register_mode
10.7
add_prefix
10.5
CRAP 17.9
« ModeMerge/Mode/Base.pm ↑ Summary ModeMerge/Mode/SUBTRACT.pm »
1 ## no critic: Modules::ProhibitAutomaticExportation
2 package Data::ModeMerge;
3  
4 32 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
5 32 our $DATE = '2021-08-15'; # DATE
6 32 our $DIST = 'Data-ModeMerge'; # DIST
7 32 our $VERSION = '0.360'; # VERSION
8  
9 32 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 32 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 32 require Exporter;
16 32 our @ISA = qw(Exporter);
17 32 our @EXPORT = qw(mode_merge);
18  
19 133 ▶sub mode_merge {
Subroutine mode_merge: called CC=1 SCAR=0.0
Pod documented
20 133 my ($l, $r, $config_vars) = @_;
21 133 my $mm = __PACKAGE__->new(config => $config_vars);
22 133 $mm->merge($l, $r);
23 }
24  
25 32 has config => (is => "rw");
26  
27 # hash of modename => handler
28 330 ▶has modes => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 32 has combine_rules => (is => 'rw');
31  
32 # merging process state
33 0 ▶has path => (is => "rw", default => sub { [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
34 0 ▶has errors => (is => "rw", default => sub { [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 0 ▶has mem => (is => "rw", default => sub { {} }); # for handling circular refs. {key=>{res=>[...], todo=>[sub1, ...]}, ...}
Subroutine __ANON__: not called CC=1 SCAR=1.2
36 32 has cur_mem_key => (is => "rw"); # for handling circular refs. instead of passing around this as argument, we put it here.
37  
38 1686 ▶sub _in($$) {
Subroutine _in: called CC=10 SCAR=23.0
39 1686 state $load_dmp = do { require Data::Dmp };
40 1686 my ($self, $needle, $haystack) = @_;
41 1686 ▶ return 0 unless defined($needle);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $needle 1 1685 1686
42 1685 my $r1 = ref($needle);
43 1685 ▶ my $f1 = $r1 ? Data::Dmp::dmp($needle) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$r1 ? : 8 1677 1685
44 1685 for (@$haystack) {
45 2583 my $r2 = ref($_);
46 2583 ▶ next if $r1 xor $r2;
Branch: 1 branch
BranchTrueFalseTotal
if $r1 xor $r2 5 2578 2583
Condition100%4/4
$r1 xor $r2
l&&rl&&!r!l&&r!l&&!r
14322564
MC/DC100%2/2
$r1 xor $r2
$r1 $r2
Truth table100%4/4
A xor B
A= $r1B= $r2
ABresult
110
101
011
000
47 2578 ▶ return 1 if $r2 && $f1 eq Data::Dmp::dmp($_);
Branch: 1 branch
BranchTrueFalseTotal
if $r2 and $f1 eq Data::Dmp::dmp($_) 5 2573 2578
Condition100%3/3
$r2 and $f1 eq Data::Dmp::dmp($_)
!ll&&!rl&&r
256495
MC/DC100%2/2
$r2 and $f1 eq Data::Dmp::dmp($_)
$r2 $f1 eq Data::Dmp::dmp($_)
Truth table100%3/3
A and B
A= $r2B= $f1 eq Data::Dmp::dmp($_)
ABresult
0X0
100
111
48 2573 ▶ return 1 if !$r2 && $needle eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if not $r2 and $needle eq $_ 357 2216 2573
Condition100%3/3
not $r2 and $needle eq $_
!ll&&!rl&&r
92207357
MC/DC100%2/2
not $r2 and $needle eq $_
not $r2 $needle eq $_
Truth table100%3/3
not(A) and B
A= not $r2B= $needle eq $_
ABresult
0X0
100
111
49 }
50 1323 0;
51 }
52  
53 330 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=20.2
Pod undocumented
54 330 require Data::ModeMerge::Config;
55  
56 330 my ($self, $args) = @_;
57  
58 330 ▶ if ($self->config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config) { } 107 223 330
59 # some sanity checks
60 107 my $is_hashref = ref($self->config) eq 'HASH';
61 107 ▶ die "config must be a hashref or a Data::ModeMerge::Config" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $is_hashref or UNIVERSAL::isa($self->config, "Data::ModeMerge::Config") 0 107 107
Condition33%1/3
$is_hashref or UNIVERSAL::isa($self->config, "Data::ModeMerge::Config")
l!l&&r!l&&!r
10700
MC/DC0%0/2
$is_hashref or UNIVERSAL::isa($self->config, "Data::ModeMerge::Config")
$is_hashref UNIVERSAL::isa($self->config, "Data::ModeMerge::Config")
Truth table33%1/3
A or B
A= $is_hashrefB= UNIVERSAL::isa($self->config, "Data::ModeMerge::Config")
ABresult
1X1
011
000
62 $is_hashref || UNIVERSAL::isa($self->config, "Data::ModeMerge::Config");
63 107 ▶ $self->config(Data::ModeMerge::Config->new(%{ $self->config })) if $is_hashref;
Branch: 1 branch
BranchTrueFalseTotal
if $is_hashref 107 0 107
64 } else {
65 223 $self->config(Data::ModeMerge::Config->new);
66 }
67  
68 330 for (qw(NORMAL KEEP ADD CONCAT SUBTRACT DELETE)) {
69 1980 $self->register_mode($_);
70 }
71  
72 330 ▶ if (!$self->combine_rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->combine_rules) 330 0 330
73 330 $self->combine_rules({
74 # "left + right" => [which mode to use, which mode after merge]
75 'ADD+ADD' => ['ADD' , 'ADD' ],
76 #'ADD+CONCAT' => undef,
77 'ADD+DELETE' => ['DELETE' , 'DELETE'],
78 #'ADD+KEEP' => undef,
79 'ADD+NORMAL' => ['NORMAL' , 'NORMAL'],
80 'ADD+SUBTRACT' => ['SUBTRACT', 'ADD' ],
81  
82 #'CONCAT+ADD' => undef,
83 'CONCAT+CONCAT' => ['CONCAT' , 'CONCAT'],
84 'CONCAT+DELETE' => ['DELETE' , 'DELETE'],
85 #'CONCAT+KEEP' => undef,
86 'CONCAT+NORMAL' => ['NORMAL' , 'NORMAL'],
87 #'CONCAT+SUBTRACT' => undef,
88  
89 'DELETE+ADD' => ['NORMAL' , 'ADD' ],
90 'DELETE+CONCAT' => ['NORMAL' , 'CONCAT' ],
91 'DELETE+DELETE' => ['DELETE' , 'DELETE' ],
92 'DELETE+KEEP' => ['NORMAL' , 'KEEP' ],
93 'DELETE+NORMAL' => ['NORMAL' , 'NORMAL' ],
94 'DELETE+SUBTRACT' => ['NORMAL' , 'SUBTRACT'],
95  
96 'KEEP+ADD' => ['KEEP', 'KEEP'],
97 'KEEP+CONCAT' => ['KEEP', 'KEEP'],
98 'KEEP+DELETE' => ['KEEP', 'KEEP'],
99 'KEEP+KEEP' => ['KEEP', 'KEEP'],
100 'KEEP+NORMAL' => ['KEEP', 'KEEP'],
101 'KEEP+SUBTRACT' => ['KEEP', 'KEEP'],
102  
103 'NORMAL+ADD' => ['ADD' , 'NORMAL'],
104 'NORMAL+CONCAT' => ['CONCAT' , 'NORMAL'],
105 'NORMAL+DELETE' => ['DELETE' , 'NORMAL'],
106 'NORMAL+KEEP' => ['NORMAL' , 'KEEP' ],
107 'NORMAL+NORMAL' => ['NORMAL' , 'NORMAL'],
108 'NORMAL+SUBTRACT' => ['SUBTRACT', 'NORMAL'],
109  
110 'SUBTRACT+ADD' => ['SUBTRACT', 'SUBTRACT'],
111 #'SUBTRACT+CONCAT' => undef,
112 'SUBTRACT+DELETE' => ['DELETE' , 'DELETE' ],
113 #'SUBTRACT+KEEP' => undef,
114 'SUBTRACT+NORMAL' => ['NORMAL' , 'NORMAL' ],
115 'SUBTRACT+SUBTRACT' => ['ADD' , 'SUBTRACT'],
116 });
117 }
118 }
119  
120 89 ▶sub push_error {
Subroutine push_error: called CC=1 SCAR=0.0
Pod documented
121 89 my ($self, $errmsg) = @_;
122 89 push @{ $self->errors }, [[@{ $self->path }], $errmsg];
123 89 return;
124 }
125  
126 1980 ▶sub register_mode {
Subroutine register_mode: called CC=7 SCAR=23.7
Pod documented
127 1980 my ($self, $name0) = @_;
128 1980 my $obj;
129 1980 ▶ if (ref($name0)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $name0) { } 0 1980 1980
elsif ($name0 =~ /^\w+(::\w+)+$/) { } 0 1980 1980
elsif ($name0 =~ /^\w+$/) { } 1980 0 1980
130 0 my $obj = $name0;
131 } elsif ($name0 =~ /^\w+(::\w+)+$/) {
132 0 eval "require $name0; \$obj = $name0->new";
133 0 ▶ die "Can't load module $name0: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
134 } elsif ($name0 =~ /^\w+$/) {
135 1980 my $modname = "Data::ModeMerge::Mode::$name0";
136 1980 eval "require $modname; \$obj = $modname->new";
137 1980 ▶ die "Can't load module $modname: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1980 1980
138 } else {
139 0 die "Invalid mode name $name0";
140 }
141 1980 my $name = $obj->name;
142 1980 ▶ die "Mode $name already registered" if $self->modes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->modes->{$name} 0 1980 1980
143 1980 $obj->merger($self);
144 1980 $self->modes->{$name} = $obj;
145 }
146  
147 18 ▶sub check_prefix {
Subroutine check_prefix: called CC=7 SCAR=19.7
Pod documented
148 18 my ($self, $hash_key) = @_;
149 18 ▶ die "Hash key not a string" if ref($hash_key);
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash_key 2 16 18
150 16 my $dis = $self->config->disable_modes;
151 16 ▶ if (defined($dis) && ref($dis) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dis and ref $dis ne "ARRAY") 0 16 16
Condition66%2/3
defined $dis and ref $dis ne "ARRAY"
!ll&&!rl&&r
1060
MC/DC0%0/2
defined $dis and ref $dis ne "ARRAY"
defined $dis ref $dis ne "ARRAY"
Truth table66%2/3
A and B
A= defined $disB= ref $dis ne "ARRAY"
ABresult
0X0
100
111
152 0 $self->push_error("Invalid config value `disable_modes`: must be an array");
153 0 return;
154 }
155 16 for my $mh (sort { $b->precedence_level <=> $a->precedence_level }
156 96 ▶ grep { !$dis || !$self->_in($_->name, $dis) }
Condition100%3/3
$dis && $self->_in($_->name, $dis)
!ll&&!rl&&r
60306
MC/DC100%2/2
$dis && $self->_in($_->name, $dis)
$dis $self->_in($_->name, $dis)
Truth table100%3/3
A && B
A= $disB= $self->_in($_->name, $dis)
ABresult
0X0
100
111
157 16 values %{ $self->modes }) {
158 80 ▶ if ($mh->check_prefix($hash_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mh->check_prefix($hash_key)) 5 75 80
159 5 return $mh->name;
160 }
161 }
162 11 return;
163 }
164  
165 7 ▶sub check_prefix_on_hash {
Subroutine check_prefix_on_hash: called CC=4 SCAR=13.9
Pod documented
166 7 my ($self, $hash) = @_;
167 7 ▶ die "Not a hash" unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 2 5 7
168 5 my $res = 0;
169 5 for (keys %$hash) {
170 7 ▶ do { $res++; last } if $self->check_prefix($_);
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_prefix($_)) 2 5 7
171 }
172 5 $res;
173 }
174  
175 36 ▶sub add_prefix {
Subroutine add_prefix: called CC=7 SCAR=23.6
Pod documented
176 36 my ($self, $hash_key, $mode) = @_;
177 36 ▶ die "Hash key not a string" if ref($hash_key);
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash_key 2 34 36
178 34 my $dis = $self->config->disable_modes;
179 34 ▶ if (defined($dis) && ref($dis) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dis and ref $dis ne "ARRAY") 0 34 34
Condition33%1/3
defined $dis and ref $dis ne "ARRAY"
!ll&&!rl&&r
3400
MC/DC0%0/2
defined $dis and ref $dis ne "ARRAY"
defined $dis ref $dis ne "ARRAY"
Truth table33%1/3
A and B
A= defined $disB= ref $dis ne "ARRAY"
ABresult
0X0
100
111
180 0 die "Invalid config value `disable_modes`: must be an array";
181 }
182 34 ▶ if ($dis && $self->_in($mode, $dis)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dis and $self->_in($mode, $dis)) 0 34 34
Condition33%1/3
$dis and $self->_in($mode, $dis)
!ll&&!rl&&r
3400
MC/DC0%0/2
$dis and $self->_in($mode, $dis)
$dis $self->_in($mode, $dis)
Truth table33%1/3
A and B
A= $disB= $self->_in($mode, $dis)
ABresult
0X0
100
111
183 0 $self->push_error("Can't add prefix for currently disabled mode `$mode`");
184 0 return $hash_key;
185 }
186 34 ▶ my $mh = $self->modes->{$mode} or die "Unknown mode: $mode";
Branch: 1 branch
BranchTrueFalseTotal
my $mh = $self->modes->{$mode} or die "Unknown mode: $mode" 34 0 34
187 34 $mh->add_prefix($hash_key);
188 }
189  
190 2293 ▶sub remove_prefix {
Subroutine remove_prefix: called CC=9 SCAR=22.0
Pod documented
191 2293 my ($self, $hash_key) = @_;
192 2293 ▶ die "Hash key not a string" if ref($hash_key);
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash_key 2 2291 2293
193 2291 my $dis = $self->config->disable_modes;
194 2291 ▶ if (defined($dis) && ref($dis) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dis and ref $dis ne "ARRAY") 1 2290 2291
Condition100%3/3
defined $dis and ref $dis ne "ARRAY"
!ll&&!rl&&r
20642261
MC/DC100%2/2
defined $dis and ref $dis ne "ARRAY"
defined $dis ref $dis ne "ARRAY"
Truth table100%3/3
A and B
A= defined $disB= ref $dis ne "ARRAY"
ABresult
0X0
100
111
195 1 die "Invalid config value `disable_modes`: must be an array";
196 }
197 2290 for my $mh (sort { $b->precedence_level <=> $a->precedence_level }
198 13740 ▶ grep { !$dis || !$self->_in($_->name, $dis) }
Condition100%3/3
$dis && $self->_in($_->name, $dis)
!ll&&!rl&&r
123841102254
MC/DC100%2/2
$dis && $self->_in($_->name, $dis)
$dis $self->_in($_->name, $dis)
Truth table100%3/3
A && B
A= $disB= $self->_in($_->name, $dis)
ABresult
0X0
100
111
199 2290 values %{ $self->modes }) {
200 11372 ▶ if ($mh->check_prefix($hash_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mh->check_prefix($hash_key)) 912 10460 11372
201 912 my $r = $mh->remove_prefix($hash_key);
202 912 ▶ if (wantarray) { return ($r, $mh->name) }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 909 3 912
203 3 else { return $r }
204 }
205 }
206 1378 ▶ if (wantarray) { return ($hash_key, $self->config->default_mode) }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1377 1 1378
207 1 else { return $hash_key }
208 }
209  
210 3 ▶sub remove_prefix_on_hash {
Subroutine remove_prefix_on_hash: called CC=5 SCAR=16.1
Pod documented
211 3 my ($self, $hash) = @_;
212 3 ▶ die "Not a hash" unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 2 1 3
213 1 for (keys %$hash) {
214 4 my $old = $_;
215 4 $_ = $self->remove_prefix($_);
216 4 ▶ next unless $old ne $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $old ne $_ 1 3 4
217 die "Conflict when removing prefix on hash: $old -> $_ but $_ already exists"
218 3 ▶ if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 0 3 3
219 3 $hash->{$_} = $hash->{$old};
220 3 delete $hash->{$old};
221 }
222 1 $hash;
223 }
224  
225 323 ▶sub merge {
Subroutine merge: called CC=2 SCAR=6.9
Pod documented
226 323 my ($self, $l, $r) = @_;
227 323 $self->path([]);
228 323 $self->errors([]);
229 323 $self->mem({});
230 323 $self->cur_mem_key(undef);
231 323 my ($key, $res, $backup) = $self->_merge(undef, $l, $r);
232 {
233 322 ▶ success => !@{ $self->errors },
Branch: 1 branch
BranchTrueFalseTotal
@{$self->errors;} ? : 89 233 322
234 322 error => (@{ $self->errors } ?
235 join(", ",
236 89 map { sprintf("/%s: %s", join("/", @{ $_->[0] }), $_->[1]) }
237 322 @{ $self->errors }) : ''),
238 result => $res,
239 backup => $backup,
240 };
241 }
242  
243 # handle circular refs: process todo's
244 755 ▶sub _process_todo {
Subroutine _process_todo: called CC=7 SCAR=19.5
245 755 my ($self) = @_;
246 755 ▶ if ($self->cur_mem_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cur_mem_key) 746 9 755
247 746 for my $mk (keys %{ $self->mem }) {
248 1031 my $res = $self->mem->{$mk}{res};
249 1031 ▶ if (defined($res) && @{ $self->mem->{$mk}{todo} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and @{$self->mem->{$mk}{"todo"};}) 2 1029 1031
Condition100%3/3
defined $res and @{$self->mem->{$mk}{"todo"};}
!ll&&!rl&&r
4885412
MC/DC100%2/2
defined $res and @{$self->mem->{$mk}{"todo"};}
defined $res @{$self->mem->{$mk}{"todo"};}
Truth table100%3/3
A and B
A= defined $resB= @{$self->mem->{$mk}{"todo"};}
ABresult
0X0
100
111
250 #print "DEBUG: processing todo for mem<$mk>\n";
251 2 for (@{ $self->mem->{$mk}{todo} }) {
252 2 $_->(@$res);
253 2 ▶ return if @{ $self->errors };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->errors;} 0 2 2
254 }
255 2 $self->mem->{$mk}{todo} = [];
256 }
257 }
258 }
259 }
260  
261 776 ▶sub _merge {
Subroutine _merge: called CC=37 SCAR=37.2
262 776 my ($self, $key, $l, $r, $mode) = @_;
263 776 my $c = $self->config;
264 776 ▶ $mode //= $c->default_mode;
Condition66%2/3
$mode //= $c->default_mode
l!l&&r!l&&!r
4533230
MC/DC0%0/2
$mode //= $c->default_mode
$mode $c->default_mode
Truth table66%2/3
A //= B
A= $modeB= $c->default_mode
ABresult
1X1
011
000
265  
266 776 my $mh = $self->modes->{$mode};
267 776 ▶ die "Can't find handler for mode $mode" unless $mh;
Branch: 1 branch
BranchTrueFalseTotal
unless $mh 0 776 776
268  
269 # determine which merge method we will call
270 776 my $rl = ref($l);
271 776 my $rr = ref($r);
272 776 ▶ my $tl = $rl eq 'HASH' ? 'HASH' : $rl eq 'ARRAY' ? 'ARRAY' : $rl eq 'CODE' ? 'CODE' : !$rl ? 'SCALAR' : '';
Branch: 4 branches
BranchTrueFalseTotal
$rl eq 'HASH' ? : 343 433 776
$rl eq 'ARRAY' ? : 52 381 433
$rl eq 'CODE' ? : 4 377 381
!$rl ? : 377 0 377
273 776 ▶ my $tr = $rr eq 'HASH' ? 'HASH' : $rr eq 'ARRAY' ? 'ARRAY' : $rr eq 'CODE' ? 'CODE' : !$rr ? 'SCALAR' : '';
Branch: 4 branches
BranchTrueFalseTotal
$rr eq 'HASH' ? : 340 436 776
$rr eq 'ARRAY' ? : 53 383 436
$rr eq 'CODE' ? : 4 379 383
!$rr ? : 379 0 379
274 0 ▶ if (!$tl) { $self->push_error("Unknown type in left side: $rl"); return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tl) 0 776 776
275 0 ▶ if (!$tr) { $self->push_error("Unknown type in right side: $rr"); return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tr) 0 776 776
276 776 ▶ if (!$c->allow_create_array && $tl ne 'ARRAY' && $tr eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->allow_create_array and $tl ne "ARRAY" and $tr eq "ARRAY") 5 771 776
Condition83%5/6
not $c->allow_create_array and $tl ne "ARRAY" and $tr eq "ARRAY"
!ll&&!rl&&r
77105
not $c->allow_create_array and $tl ne "ARRAY"
!ll&&!rl&&r
77015
MC/DC66%2/3
not $c->allow_create_array and $tl ne "ARRAY" and $tr eq "ARRAY"
not $c->allow_create_array $tl ne "ARRAY" $tr eq "ARRAY"
Truth table75%3/4
not(A) and B and C
A= not $c->allow_create_arrayB= $tl ne "ARRAY"C= $tr eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
277 5 $self->push_error("Not allowed to create array"); return;
278 }
279 771 ▶ if (!$c->allow_create_hash && $tl ne 'HASH' && $tr eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->allow_create_hash and $tl ne "HASH" and $tr eq "HASH") 5 766 771
Condition83%5/6
not $c->allow_create_hash and $tl ne "HASH" and $tr eq "HASH"
!ll&&!rl&&r
76605
not $c->allow_create_hash and $tl ne "HASH"
!ll&&!rl&&r
76515
MC/DC66%2/3
not $c->allow_create_hash and $tl ne "HASH" and $tr eq "HASH"
not $c->allow_create_hash $tl ne "HASH" $tr eq "HASH"
Truth table75%3/4
not(A) and B and C
A= not $c->allow_create_hashB= $tl ne "HASH"C= $tr eq "HASH"
ABCresult
0XX0
10X0
1100
1111
280 5 $self->push_error("Not allowed to create hash"); return;
281 }
282 766 ▶ if (!$c->allow_destroy_array && $tl eq 'ARRAY' && $tr ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->allow_destroy_array and $tl eq "ARRAY" and $tr ne "ARRAY") 5 761 766
Condition100%6/6
not $c->allow_destroy_array and $tl eq "ARRAY" and $tr ne "ARRAY"
!ll&&!rl&&r
75925
not $c->allow_destroy_array and $tl eq "ARRAY"
!ll&&!rl&&r
75817
MC/DC100%3/3
not $c->allow_destroy_array and $tl eq "ARRAY" and $tr ne "ARRAY"
not $c->allow_destroy_array $tl eq "ARRAY" $tr ne "ARRAY"
Truth table100%4/4
not(A) and B and C
A= not $c->allow_destroy_arrayB= $tl eq "ARRAY"C= $tr ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
283 5 $self->push_error("Not allowed to destroy array"); return;
284 }
285 761 ▶ if (!$c->allow_destroy_hash && $tl eq 'HASH' && $tr ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->allow_destroy_hash and $tl eq "HASH" and $tr ne "HASH") 5 756 761
Condition83%5/6
not $c->allow_destroy_hash and $tl eq "HASH" and $tr ne "HASH"
!ll&&!rl&&r
75335
not $c->allow_destroy_hash and $tl eq "HASH"
!ll&&!rl&&r
75308
MC/DC66%2/3
not $c->allow_destroy_hash and $tl eq "HASH" and $tr ne "HASH"
not $c->allow_destroy_hash $tl eq "HASH" $tr ne "HASH"
Truth table75%3/4
not(A) and B and C
A= not $c->allow_destroy_hashB= $tl eq "HASH"C= $tr ne "HASH"
ABCresult
0XX0
10X0
1100
1111
286 5 $self->push_error("Not allowed to destroy hash"); return;
287 }
288 756 my $meth = "merge_${tl}_${tr}";
289 0 ▶ if (!$mh->can($meth)) { $self->push_error("No merge method found for $tl + $tr (mode $mode)"); return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($mh->can($meth)) 0 756 756
290  
291 #$self->_process_todo;
292 # handle circular refs: add to todo if necessary
293 756 my $memkey;
294 756 ▶ if ($rl || $rr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rl or $rr) 406 350 756
Condition100%3/3
$rl or $rr
l!l&&r!l&&!r
38719350
MC/DC100%2/2
$rl or $rr
$rl $rr
Truth table100%3/3
A or B
A= $rlB= $rr
ABresult
1X1
011
000
295 406 ▶ $memkey = sprintf "%s%s %s%s %s %s",
Branch: 6 branches
BranchTrueFalseTotal
defined $l ? : 402 4 406
$rl ? : 387 15 402
defined $l ? : 402 4 406
defined $r ? : 400 6 406
$rr ? : 385 15 400
defined $r ? : 400 6 406
296 (defined($l) ? ($rl ? 2 : 1) : 0),
297 (defined($l) ? "$l" : ''),
298 (defined($r) ? ($rr ? 2 : 1) : 0),
299 (defined($r) ? "$r" : ''),
300 $mode,
301 $self->config;
302 #print "DEBUG: number of keys in mem = ".scalar(keys %{ $self->mem })."\n";
303 #print "DEBUG: mem keys = \n".join("", map { " $_\n" } keys %{ $self->mem }) if keys %{ $self->mem };
304 #print "DEBUG: calculating memkey = <$memkey>\n";
305 }
306 756 ▶ if ($memkey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($memkey) { } 406 350 756
307 406 ▶ if (exists $self->mem->{$memkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->mem->{$memkey}) { } 3 403 406
308 3 $self->_process_todo;
309 3 ▶ if (defined $self->mem->{$memkey}{res}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->mem->{$memkey}{'res'}) { } 0 3 3
310 #print "DEBUG: already calculated, using cached result\n";
311 0 return @{ $self->mem->{$memkey}{res} };
312 } else {
313 #print "DEBUG: detecting circular\n";
314 3 return ($key, undef, undef, 1);
315 }
316 } else {
317 403 $self->mem->{$memkey} = {res=>undef, todo=>[]};
318 403 $self->cur_mem_key($memkey);
319 #print "DEBUG: invoking ".$mh->name."'s $meth(".dmp($key).", ".dmp($l).", ".dmp($r).")\n";
320 403 my ($newkey, $res, $backup) = $mh->$meth($key, $l, $r);
321 #print "DEBUG: setting res for mem<$memkey>\n";
322 402 $self->mem->{$memkey}{res} = [$newkey, $res, $backup];
323 402 $self->_process_todo;
324 402 return ($newkey, $res, $backup);
325 }
326 } else {
327 350 $self->_process_todo;
328 #print "DEBUG: invoking ".$mh->name."'s $meth(".dmp($key).", ".dmp($l).", ".dmp($r).")\n";
329 350 return $mh->$meth($key, $l, $r);
330 }
331 }
332  
333 # returns 1 if a is included in b (e.g. [user => "jajang"] in included in [user
334 # => jajang => "quota"], but [user => "paijo"] is not)
335 5 ▶sub _path_is_included {
Subroutine _path_is_included: called CC=4 SCAR=13.9
336 5 my ($self, $p1, $p2) = @_;
337 5 my $res = 1;
338 5 for my $i (0..@$p1-1) {
339 6 ▶ do { $res = 0; last } if !defined($p2->[$i]) || $p1->[$i] ne $p2->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $p2->[$i] or $p1->[$i] ne $p2->[$i]) 2 4 6
Condition66%2/3
not defined $p2->[$i] or $p1->[$i] ne $p2->[$i]
l!l&&r!l&&!r
024
MC/DC50%1/2
not defined $p2->[$i] or $p1->[$i] ne $p2->[$i]
not defined $p2->[$i] $p1->[$i] ne $p2->[$i]
Truth table66%2/3
not(A) or B
A= not defined $p2->[$i]B= $p1->[$i] ne $p2->[$i]
ABresult
1X1
011
000
340 }
341 #print "_path_is_included([".join(", ", @$p1)."], [".join(", ", @$p2)."])? $res\n";
342 5 $res;
343 }
344  
345 32 1;
346 # ABSTRACT: Merge two nested data structures, with merging modes and options
347  
348 __END__
« ModeMerge/Mode/Base.pm ↑ Summary ModeMerge/Mode/SUBTRACT.pm »