Smart/Engine.pm

filter: Statementstmt 100% 211 / 211 Branchbran 96.0% 123 / 128 Conditioncond 93.1% 82 / 88 MC/DCmcdc 90.0% 45 / 50 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 96.7% 480 / 496 CC 117 SCAR 28.1
CC
117
Cov
97%
_diff_array_lcs
33.0
_diff_array_unordered
21.0
_diff
16.0
CRAP 16.7
↑ Summary Smart/Renderer/Test2.pm »
1 package Data::Hash::Diff::Smart::Engine;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 ▶use Scalar::Util qw(reftype blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Data::Hash::Diff::Smart::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =head1 NAME
12  
13 Data::Hash::Diff::Smart::Engine - Internal diff engine for Data::Hash::Diff::Smart
14  
15 =head1 VERSION
16  
17 Version 0.02
18  
19 =cut
20  
21 16 our $VERSION = '0.02';
22  
23 =head1 DESCRIPTION
24  
25 This module implements the recursive diff algorithm used by L<Data::Hash::Diff::Smart>.
26 It is not intended to be used directly.
27  
28 Features include:
29  
30 =over 4
31  
32 =item * recursive comparison of scalars, hashes, arrays, and objects
33  
34 =item * cycle detection to avoid infinite recursion
35  
36 =item * ignore rules (exact, regex, wildcard)
37  
38 =item * custom comparators per path
39  
40 =item * array diff modes: index, LCS, unordered
41  
42 =back
43  
44 =head1 INTERNAL METHODS
45  
46 =head2 diff($old, $new, %opts)
47  
48 Entry point for computing a diff.
49  
50 =head2 _diff($old, $new, $path, $changes, $ctx)
51  
52 Recursive comparison routine.
53  
54 =head2 _diff_scalar, _diff_hash, _diff_array
55  
56 Type-specific comparison helpers.
57  
58 =head2 _diff_array_index, _diff_array_lcs, _diff_array_unordered
59  
60 Array diffing strategies.
61  
62 =head2 _normalize_ignore, _is_ignored
63  
64 Ignore rule processing.
65  
66 =head2 _reftype, _eq
67  
68 Utility helpers.
69  
70 =cut
71  
72 # -------------------------------------------------------------------------
73 # Public entry point
74 # -------------------------------------------------------------------------
75  
76 430 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.0
Pod documented
77 430 my ($old, $new, %opts) = @_;
78  
79 430 my $changes = [];
80  
81 my $ctx = {
82 ignore => _normalize_ignore($opts{ignore}),
83 compare => $opts{compare} || {},
84 array_mode => $opts{array_mode} || 'index',
85 array_key => $opts{array_key}
86 430 ▶ };
Condition100%4/4
$opts{'compare'} || {}
l!l
32398
$opts{'array_mode'} || 'index'
l!l
92338
MC/DC100%1/1
$opts{'compare'} || {}
$opts{'compare'}
MC/DC100%1/1
$opts{'array_mode'} || 'index'
$opts{'array_mode'}
Truth table100%2/2
A
A= $opts{'compare'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'array_mode'}
Aresult
11
00
87  
88 430 _diff($old, $new, '', $changes, $ctx);
89  
90 429 return $changes;
91 }
92  
93 # -------------------------------------------------------------------------
94 # Core recursive diff
95 # -------------------------------------------------------------------------
96  
97 114325 ▶sub _diff {
Subroutine _diff: called CC=16 SCAR=27.7
98 114325 my ($old, $new, $path, $changes, $ctx) = @_;
99  
100 # Ignore rules
101 114325 ▶ return if _is_ignored($path, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
if _is_ignored($path, $ctx->{"ignore"}) 63 114262 114325
102  
103 # ------------------------------------------------------------------
104 # Cycle detection
105 # ------------------------------------------------------------------
106 114262 ▶ if (ref($old) && ref($new)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old and ref $new) 11779 102483 114262
Condition100%3/3
ref $old and ref $new
!ll&&!rl&&r
102482111779
MC/DC100%2/2
ref $old and ref $new
ref $old ref $new
Truth table100%3/3
A and B
A= ref $oldB= ref $new
ABresult
0X0
100
111
107 11779 my $ro = refaddr($old);
108 11779 my $rn = refaddr($new);
109  
110 # If we've seen this pair before, stop recursion
111 11779 ▶ if ($ctx->{seen}{$ro}{$rn}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"seen"}{$ro}{$rn}++) 19 11760 11779
112 19 return;
113 }
114 }
115  
116 114243 my $rt_old = _reftype($old);
117 114243 my $rt_new = _reftype($new);
118  
119 # ------------------------------------------------------------------
120 # Both scalars
121 # ------------------------------------------------------------------
122 114243 ▶ if (!$rt_old && !$rt_new) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rt_old or $rt_new) 102475 11768 114243
Condition100%3/3
$rt_old or $rt_new
l!l&&r!l&&!r
117617102475
MC/DC100%2/2
$rt_old or $rt_new
$rt_old $rt_new
Truth table100%3/3
A or B
A= $rt_oldB= $rt_new
ABresult
1X1
011
000
123 102475 return _diff_scalar($old, $new, $path, $changes, $ctx);
124 }
125  
126 # ------------------------------------------------------------------
127 # Type mismatch
128 # ------------------------------------------------------------------
129 11768 ▶ if ($rt_old && $rt_new && $rt_old ne $rt_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt_old and $rt_new and $rt_old ne $rt_new) 5 11763 11768
Condition100%6/6
$rt_old and $rt_new and $rt_old ne $rt_new
!ll&&!rl&&r
8117555
$rt_old and $rt_new
!ll&&!rl&&r
7111760
MC/DC100%3/3
$rt_old and $rt_new and $rt_old ne $rt_new
$rt_old $rt_new $rt_old ne $rt_new
Truth table100%4/4
A and B and C
A= $rt_oldB= $rt_newC= $rt_old ne $rt_new
ABCresult
0XX0
10X0
1100
1111
130 5 push @$changes, {
131 op => 'change',
132 path => $path,
133 from => $old,
134 to => $new,
135 };
136 5 return;
137 }
138  
139 # ------------------------------------------------------------------
140 # One ref, one scalar
141 # ------------------------------------------------------------------
142 11763 ▶ if ($rt_old && !$rt_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt_old and not $rt_new) 1 11762 11763
Condition100%3/3
$rt_old and not $rt_new
!ll&&!rl&&r
7117551
MC/DC100%2/2
$rt_old and not $rt_new
$rt_old not $rt_new
Truth table100%3/3
A and not(B)
A= $rt_oldB= not $rt_new
ABresult
0X0
100
111
143 1 push @$changes, {
144 op => 'change',
145 path => $path,
146 from => $old,
147 to => $new,
148 };
149 1 return;
150 }
151  
152 11762 ▶ if (!$rt_old && $rt_new) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rt_old and $rt_new) 7 11755 11762
Condition66%2/3
not $rt_old and $rt_new
!ll&&!rl&&r
1175507
MC/DC50%1/2
not $rt_old and $rt_new
not $rt_old $rt_new
Truth table66%2/3
not(A) and B
A= not $rt_oldB= $rt_new
ABresult
0X0
100
111
153 7 push @$changes, {
154 op => 'change',
155 path => $path,
156 from => $old,
157 to => $new,
158 };
159 7 return;
160 }
161  
162 # ------------------------------------------------------------------
163 # Both refs, same type
164 # ------------------------------------------------------------------
165 11755 ▶ if ($rt_old eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt_old eq "HASH") 11611 144 11755
166 11611 return _diff_hash($old, $new, $path, $changes, $ctx);
167 }
168  
169 144 ▶ if ($rt_old eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt_old eq "ARRAY") 134 10 144
170 134 return _diff_array($old, $new, $path, $changes, $ctx);
171 }
172  
173 # ------------------------------------------------------------------
174 # Fallback: stringify
175 # ------------------------------------------------------------------
176 10 return _diff_scalar("$old", "$new", $path, $changes, $ctx);
177 }
178  
179 # -------------------------------------------------------------------------
180 # Scalar comparison
181 # -------------------------------------------------------------------------
182  
183 102492 ▶sub _diff_scalar {
Subroutine _diff_scalar: called CC=5 SCAR=16.1
184 102492 my ($old, $new, $path, $changes, $ctx) = @_;
185  
186 # Custom comparator?
187 102492 ▶ if (my $cmp = $ctx->{compare}{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmp = $ctx->{'compare'}{$path}) { } 31 102461 102492
188 31 my $same = eval { $cmp->($old, $new) };
189 31 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 27 31
190 4 push @$changes, {
191 op => 'change',
192 path => $path,
193 from => $old,
194 to => $new,
195 error => "$@",
196 };
197 4 return;
198 }
199 27 ▶ return if $same;
Branch: 1 branch
BranchTrueFalseTotal
if $same 18 9 27
200 }
201 else {
202 102461 ▶ return if _eq($old, $new);
Branch: 1 branch
BranchTrueFalseTotal
if _eq($old, $new) 2112 100349 102461
203 }
204  
205 100358 push @$changes, {
206 op => 'change',
207 path => $path,
208 from => $old,
209 to => $new,
210 };
211 }
212  
213 # -------------------------------------------------------------------------
214 # Hash comparison
215 # -------------------------------------------------------------------------
216  
217 11620 ▶sub _diff_hash {
Subroutine _diff_hash: called CC=9 SCAR=22.0
218 11620 my ($old, $new, $path, $changes, $ctx) = @_;
219  
220 11620 my %keys;
221 11620 $keys{$_}++ for keys %$old;
222 11620 $keys{$_}++ for keys %$new;
223  
224 11620 for my $k (sort keys %keys) {
225 112539 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $k);
226  
227 112539 ▶ if (exists $old->{$k} && exists $new->{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $old->{$k} and exists $new->{$k}) { } 112465 74 112539
elsif (exists $old->{$k}) { } 37 37 74
Condition100%3/3
exists $old->{$k} and exists $new->{$k}
!ll&&!rl&&r
3737112465
MC/DC100%2/2
exists $old->{$k} and exists $new->{$k}
exists $old->{$k} exists $new->{$k}
Truth table100%3/3
A and B
A= exists $old->{$k}B= exists $new->{$k}
ABresult
0X0
100
111
228 112465 _diff($old->{$k}, $new->{$k}, $subpath, $changes, $ctx);
229 } elsif (exists $old->{$k}) {
230 push @$changes, {
231 op => 'remove',
232 path => $subpath,
233 from => $old->{$k},
234 37 ▶ } unless _is_ignored($subpath, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ignored($subpath, $ctx->{"ignore"}) 36 1 37
235 } else {
236 push @$changes, {
237 op => 'add',
238 path => $subpath,
239 value => $new->{$k},
240 37 ▶ } unless _is_ignored($subpath, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ignored($subpath, $ctx->{"ignore"}) 36 1 37
241 }
242 }
243 }
244  
245 # -------------------------------------------------------------------------
246 # Array comparison dispatcher
247 # -------------------------------------------------------------------------
248  
249 134 ▶sub _diff_array {
Subroutine _diff_array: called CC=5 SCAR=16.1
250 134 my ($old, $new, $path, $changes, $ctx) = @_;
251  
252 134 ▶ my $mode = $ctx->{array_mode} || 'index';
Condition50%1/2
$ctx->{'array_mode'} || 'index'
l!l
1340
MC/DC0%0/1
$ctx->{'array_mode'} || 'index'
$ctx->{'array_mode'}
Truth table50%1/2
A
A= $ctx->{'array_mode'}
Aresult
11
00
253  
254 134 ▶ if ($mode eq 'index') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'index') { } 55 79 134
elsif ($mode eq 'lcs') { } 29 50 79
elsif ($mode eq 'unordered') { } 49 1 50
255 55 return _diff_array_index($old, $new, $path, $changes, $ctx);
256 } elsif ($mode eq 'lcs') {
257 29 return _diff_array_lcs($old, $new, $path, $changes, $ctx);
258 } elsif ($mode eq 'unordered') {
259 49 return _diff_array_unordered($old, $new, $path, $changes, $ctx);
260 }
261  
262 1 die "Unsupported array_mode: $mode";
263 }
264  
265 # -------------------------------------------------------------------------
266 # Array mode: index
267 # -------------------------------------------------------------------------
268  
269 61 ▶sub _diff_array_index {
Subroutine _diff_array_index: called CC=8 SCAR=20.9
270 61 my ($old, $new, $path, $changes, $ctx) = @_;
271  
272 61 ▶ my $max = @$old > @$new ? @$old : @$new;
Branch: 1 branch
BranchTrueFalseTotal
@$old > @$new ? : 4 57 61
273  
274 61 for my $i (0 .. $max - 1) {
275 1146 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $i);
276  
277 1146 ▶ if ($i <= $#$old && $i <= $#$new) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $old->$#* and $i <= $new->$#*) { } 1129 17 1146
elsif ($i <= $old->$#*) { } 8 9 17
Condition100%3/3
$i <= $old->$#* and $i <= $new->$#*
!ll&&!rl&&r
981129
MC/DC100%2/2
$i <= $old->$#* and $i <= $new->$#*
$i <= $old->$#* $i <= $new->$#*
Truth table100%3/3
A and B
A= $i <= $old->$#*B= $i <= $new->$#*
ABresult
0X0
100
111
278 1129 _diff($old->[$i], $new->[$i], $subpath, $changes, $ctx);
279 } elsif ($i <= $#$old) {
280 push @$changes, { op => 'remove', path => $subpath, from => $old->[$i] }
281 8 ▶ unless _is_ignored($subpath, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ignored($subpath, $ctx->{"ignore"}) 8 0 8
282 } else {
283 push @$changes, { op => 'add', path => $subpath, value => $new->[$i] }
284 9 ▶ unless _is_ignored($subpath, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ignored($subpath, $ctx->{"ignore"}) 9 0 9
285 }
286 }
287 }
288  
289 # -------------------------------------------------------------------------
290 # Array mode: LCS (Longest Common Subsequence)
291 # -------------------------------------------------------------------------
292  
293 35 ▶sub _diff_array_lcs {
Subroutine _diff_array_lcs: called CC=33 SCAR=35.0
294 35 my ($old, $new, $path, $changes, $ctx) = @_;
295  
296 35 my @a = @$old;
297 35 my @b = @$new;
298  
299 35 my $m = @a;
300 35 my $n = @b;
301  
302 # DP table
303 35 my @dp;
304 35 for my $i (0 .. $m) {
305 314 for my $j (0 .. $n) {
306 41001 $dp[$i][$j] = 0;
307 }
308 }
309  
310 35 for my $i (1 .. $m) {
311 279 for my $j (1 .. $n) {
312 40402 ▶ if (_eq($a[$i-1], $b[$j-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_eq($a[$i - 1], $b[$j - 1])) { } 248 40154 40402
313 248 $dp[$i][$j] = $dp[$i-1][$j-1] + 1;
314 } else {
315 40154 ▶ $dp[$i][$j] = $dp[$i-1][$j] > $dp[$i][$j-1]
Branch: 1 branch
BranchTrueFalseTotal
$dp[$i - 1][$j] > $dp[$i][$j - 1] ? : 19941 20213 40154
316 ? $dp[$i-1][$j]
317 : $dp[$i][$j-1];
318 }
319 }
320 }
321  
322 # Extract LCS
323 35 my @lcs;
324 35 my ($i, $j) = ($m, $n);
325  
326 35 ▶ while ($i > 0 && $j > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $j > 0) 282 35 317
Condition100%3/3
$i > 0 and $j > 0
!ll&&!rl&&r
314282
MC/DC100%2/2
$i > 0 and $j > 0
$i > 0 $j > 0
Truth table100%3/3
A and B
A= $i > 0B= $j > 0
ABresult
0X0
100
111
327 282 ▶ if (_eq($a[$i-1], $b[$j-1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (_eq($a[$i - 1], $b[$j - 1])) { } 245 37 282
elsif ($dp[$i - 1][$j] >= $dp[$i][$j - 1]) { } 24 13 37
328 245 unshift @lcs, $a[$i-1];
329 245 $i--; $j--;
330 }
331 elsif ($dp[$i-1][$j] >= $dp[$i][$j-1]) {
332 24 $i--;
333 }
334 else {
335 13 $j--;
336 }
337 }
338  
339 # Walk arrays and LCS
340 35 my ($ai, $bi, $li) = (0, 0, 0);
341  
342 35 ▶ while ($ai < @a || $bi < @b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai < @a or $bi < @b) 288 25 313
Condition100%3/3
$ai < @a or $bi < @b
l!l&&r!l&&!r
283525
MC/DC100%2/2
$ai < @a or $bi < @b
$ai < @a $bi < @b
Truth table100%3/3
A or B
A= $ai < @aB= $bi < @b
ABresult
1X1
011
000
343 288 ▶ my $l = $li < @lcs ? $lcs[$li] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$li < @lcs ? : 261 27 288
344  
345 288 ▶ if ($ai < @a && $bi < @b && _eq($a[$ai], $b[$bi])) {
Branch: 5 branches
BranchTrueFalseTotal
if ($ai < @a and $bi < @b and _eq($a[$ai], $b[$bi])) { } 245 43 288
elsif ($ai < @a and defined $l and _eq($a[$ai], $l)) { } 11 32 43
elsif ($bi < @b and defined $l and _eq($b[$bi], $l)) { } 5 27 32
elsif ($bi >= @b) { } 5 22 27
elsif ($ai >= @a) { } 5 17 22
Condition100%6/6
$ai < @a and $bi < @b and _eq($a[$ai], $b[$bi])
!ll&&!rl&&r
1033245
$ai < @a and $bi < @b
!ll&&!rl&&r
55278
MC/DC100%3/3
$ai < @a and $bi < @b and _eq($a[$ai], $b[$bi])
$ai < @a $bi < @b _eq($a[$ai], $b[$bi])
Truth table100%4/4
A and B and C
A= $ai < @aB= $bi < @bC= _eq($a[$ai], $b[$bi])
ABCresult
0XX0
10X0
1100
1111
346 245 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $bi);
347 245 _diff($a[$ai], $b[$bi], $subpath, $changes, $ctx);
348 245 $ai++; $bi++;
349 ▶ } elsif ($ai < @a && defined $l && _eq($a[$ai], $l)) {
Condition100%6/6
$ai < @a and defined $l and _eq($a[$ai], $l)
!ll&&!rl&&r
27511
$ai < @a and defined $l
!ll&&!rl&&r
52216
MC/DC100%3/3
$ai < @a and defined $l and _eq($a[$ai], $l)
$ai < @a defined $l _eq($a[$ai], $l)
Truth table100%4/4
A and B and C
A= $ai < @aB= defined $lC= _eq($a[$ai], $l)
ABCresult
0XX0
10X0
1100
1111
350 11 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $bi);
351 11 push @$changes, {
352 op => 'add',
353 path => $subpath,
354 value => $b[$bi],
355 };
356 11 $bi++;
357 ▶ } elsif ($bi < @b && defined $l && _eq($b[$bi], $l)) {
Condition83%5/6
$bi < @b and defined $l and _eq($b[$bi], $l)
!ll&&!rl&&r
2705
$bi < @b and defined $l
!ll&&!rl&&r
5225
MC/DC66%2/3
$bi < @b and defined $l and _eq($b[$bi], $l)
$bi < @b defined $l _eq($b[$bi], $l)
Truth table75%3/4
A and B and C
A= $bi < @bB= defined $lC= _eq($b[$bi], $l)
ABCresult
0XX0
10X0
1100
1111
358 5 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $ai);
359 5 push @$changes, {
360 op => 'remove',
361 path => $subpath,
362 from => $a[$ai],
363 };
364 5 $ai++;
365 } elsif ($bi >= @b) {
366 # @b exhausted — remaining @a elements are removes
367 5 last;
368 } elsif ($ai >= @a) {
369 # @a exhausted — remaining @b elements are adds
370 5 last;
371 } else {
372 17 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $bi);
373 17 _diff($a[$ai], $b[$bi], $subpath, $changes, $ctx);
374 17 $ai++; $bi++;
375 }
376  
377 278 ▶ if ($li < @lcs && $ai > 0 && $bi > 0 && _eq($a[$ai-1], $lcs[$li])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($li < @lcs and $ai > 0 and $bi > 0 and _eq($a[$ai - 1], $lcs[$li])) 245 33 278
Condition100%9/9
$li < @lcs and $ai > 0 and $bi > 0 and _eq($a[$ai - 1], $lcs[$li])
!ll&&!rl&&r
2013245
$li < @lcs and $ai > 0 and $bi > 0
!ll&&!rl&&r
191258
$li < @lcs and $ai > 0
!ll&&!rl&&r
172259
MC/DC100%4/4
$li < @lcs and $ai > 0 and $bi > 0 and _eq($a[$ai - 1], $lcs[$li])
$li < @lcs $ai > 0 $bi > 0 _eq($a[$ai - 1], $lcs[$li])
Truth table100%5/5
A and B and C and D
A= $li < @lcsB= $ai > 0C= $bi > 0D= _eq($a[$ai - 1], $lcs[$li])
ABCDresult
0XXX0
10XX0
110X0
11100
11111
378 245 $li++;
379 }
380 }
381 35 ▶ while ($ai < @a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ai < @a) 12 35 47
382 12 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $ai);
383 push @$changes, { op => 'remove', path => $subpath, from => $a[$ai] }
384 12 ▶ unless _is_ignored($subpath, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ignored($subpath, $ctx->{"ignore"}) 12 0 12
385 12 $ai++;
386 }
387 35 ▶ while ($bi < @b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bi < @b) 12 35 47
388 12 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $bi);
389 push @$changes, { op => 'add', path => $subpath, value => $b[$bi] }
390 12 ▶ unless _is_ignored($subpath, $ctx->{ignore});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ignored($subpath, $ctx->{"ignore"}) 12 0 12
391 12 $bi++;
392 }
393 }
394  
395 # -------------------------------------------------------------------------
396 # Array mode: unordered (multiset)
397 # -------------------------------------------------------------------------
398  
399 59 ▶sub _diff_array_unordered {
Subroutine _diff_array_unordered: called CC=21 SCAR=30.4
400 59 my ($old, $new, $path, $changes, $ctx) = @_;
401  
402 59 my $key_field = $ctx->{array_key};
403  
404 59 ▶ if ($key_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_field) { } 26 33 59
405 # Key-based matching: pair elements by a nominated field value
406 26 ▶ my %old_by_key = map { $_->{$key_field} => $_ } grep { ref($_) eq 'HASH' && defined $_->{$key_field} } @$old;
Condition100%3/3
ref $_ eq 'HASH' && defined $_->{$key_field}
!ll&&!rl&&r
3244
MC/DC100%2/2
ref $_ eq 'HASH' && defined $_->{$key_field}
ref $_ eq 'HASH' defined $_->{$key_field}
Truth table100%3/3
A && B
A= ref $_ eq 'HASH'B= defined $_->{$key_field}
ABresult
0X0
100
111
407 26 ▶ my %new_by_key = map { $_->{$key_field} => $_ } grep { ref($_) eq 'HASH' && defined $_->{$key_field} } @$new;
Condition100%3/3
ref $_ eq 'HASH' && defined $_->{$key_field}
!ll&&!rl&&r
3247
MC/DC100%2/2
ref $_ eq 'HASH' && defined $_->{$key_field}
ref $_ eq 'HASH' defined $_->{$key_field}
Truth table100%3/3
A && B
A= ref $_ eq 'HASH'B= defined $_->{$key_field}
ABresult
0X0
100
111
408  
409 26 my %all_keys;
410 26 $all_keys{$_}++ for keys %old_by_key;
411 26 $all_keys{$_}++ for keys %new_by_key;
412  
413 26 for my $k (sort keys %all_keys) {
414 51 my $subpath = Data::Hash::Diff::Smart::Path::join($path, $k);
415  
416 51 ▶ if (exists $old_by_key{$k} && exists $new_by_key{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $old_by_key{$k} and exists $new_by_key{$k}) { } 39 12 51
elsif (exists $old_by_key{$k}) { } 4 8 12
Condition100%3/3
exists $old_by_key{$k} and exists $new_by_key{$k}
!ll&&!rl&&r
8439
MC/DC100%2/2
exists $old_by_key{$k} and exists $new_by_key{$k}
exists $old_by_key{$k} exists $new_by_key{$k}
Truth table100%3/3
A and B
A= exists $old_by_key{$k}B= exists $new_by_key{$k}
ABresult
0X0
100
111
417 # Both sides have this key: recurse and diff structurally
418 39 _diff($old_by_key{$k}, $new_by_key{$k}, $subpath, $changes, $ctx);
419 } elsif (exists $old_by_key{$k}) {
420 push @$changes, {
421 op => 'remove',
422 path => $subpath,
423 4 from => $old_by_key{$k},
424 };
425 } else {
426 push @$changes, {
427 op => 'add',
428 path => $subpath,
429 8 value => $new_by_key{$k},
430 };
431 }
432 }
433 } else {
434 # Fallback multiset mode: use Data::Dumper for structural key
435 33 require Data::Dumper;
436 33 local $Data::Dumper::Sortkeys = 1;
437 33 local $Data::Dumper::Indent = 0;
438 33 local $Data::Dumper::Terse = 1;
439  
440 1340 ▶ my $struct_key = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
441 1340 ▶ ref($_[0]) ? Data::Dumper::Dumper($_[0]) : $_[0]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 20 1320 1340
442 33 };
443  
444 33 my %count_old;
445 my %count_new;
446 33 $count_old{$struct_key->($_)}++ for @$old;
447 33 $count_new{$struct_key->($_)}++ for @$new;
448  
449 33 my %keys;
450 33 $keys{$_}++ for keys %count_old;
451 33 $keys{$_}++ for keys %count_new;
452  
453 33 for my $k (sort keys %keys) {
454 674 ▶ my $o = $count_old{$k} || 0;
Condition100%2/2
$count_old{$k} || 0
l!l
6677
MC/DC100%1/1
$count_old{$k} || 0
$count_old{$k}
Truth table100%2/2
A
A= $count_old{$k}
Aresult
11
00
455 674 ▶ my $n = $count_new{$k} || 0;
Condition100%2/2
$count_new{$k} || 0
l!l
66410
MC/DC100%1/1
$count_new{$k} || 0
$count_new{$k}
Truth table100%2/2
A
A= $count_new{$k}
Aresult
11
00
456  
457 674 ▶ if ($n > $o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n > $o) { } 10 664 674
elsif ($o > $n) { } 14 650 664
458 10 push @$changes, { op => 'add', path => "$path/*", value => $k } for 1 .. $n - $o;
459 } elsif ($o > $n) {
460 14 push @$changes, { op => 'remove', path => "$path/*", from => $k } for 1 .. $o - $n;
461 }
462 }
463 }
464 }
465  
466 20 ▶sub _key {
Subroutine _key: called CC=2 SCAR=6.9
467 20 my $v = $_[0];
468  
469 20 ▶ return $v unless ref($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 3 17 20
470  
471 17 require Data::Dumper;
472 17 local $Data::Dumper::Sortkeys = 1;
473 17 local $Data::Dumper::Indent = 0;
474 17 local $Data::Dumper::Terse = 1;
475 17 return Data::Dumper::Dumper($v);
476 }
477  
478 # -------------------------------------------------------------------------
479 # Helpers
480 # -------------------------------------------------------------------------
481  
482 228494 ▶sub _reftype {
Subroutine _reftype: called CC=3 SCAR=11.1
483 228494 my ($v) = @_;
484 228494 ▶ return unless ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 204961 23533 228494
485 23533 ▶ return reftype($v) || 'SCALAR';
Condition50%1/2
builtin::reftype($v) || 'SCALAR'
l!l
235330
MC/DC0%0/1
builtin::reftype($v) || 'SCALAR'
builtin::reftype($v)
Truth table50%1/2
A
A= builtin::reftype($v)
Aresult
11
00
486 }
487  
488 143713 ▶sub _eq {
Subroutine _eq: called CC=5 SCAR=16.1
489 143713 my ($a, $b) = @_;
490 143713 ▶ return 1 if !defined($a) && !defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $b 8 143705 143713
Condition100%3/3
defined $a or defined $b
l!l&&r!l&&!r
143694118
MC/DC100%2/2
defined $a or defined $b
defined $a defined $b
Truth table100%3/3
A or B
A= defined $aB= defined $b
ABresult
1X1
011
000
491 143705 ▶ return 0 if defined($a) xor defined($b);
Branch: 1 branch
BranchTrueFalseTotal
if defined $a xor defined $b 16 143689 143705
Condition75%3/4
defined $a xor defined $b
l&&rl&&!r!l&&r!l&&!r
1436895110
MC/DC100%2/2
defined $a xor defined $b
defined $a defined $b
Truth table75%3/4
A xor B
A= defined $aB= defined $b
ABresult
110
101
011
000
492 143689 return $a eq $b;
493 }
494  
495 479 ▶sub _normalize_ignore {
Subroutine _normalize_ignore: called CC=5 SCAR=16.1
496 479 my ($ignore) = @_;
497 479 ▶ return [] unless $ignore;
Branch: 1 branch
BranchTrueFalseTotal
unless $ignore 412 67 479
498  
499 67 my @rules;
500  
501 67 for my $r (@$ignore) {
502  
503 # Regex rule
504 75 ▶ if (ref($r) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "Regexp") 13 62 75
505 13 push @rules, { type => 'regex', re => $r };
506 13 next;
507 }
508  
509 # String rule: check for wildcard
510 62 ▶ if ($r =~ /\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /\*/) { } 15 47 62
511 15 my @parts = grep { length $_ } split m{/}, $r;
512 15 push @rules, { type => 'wildcard', parts => \@parts };
513 }
514 else {
515 47 push @rules, { type => 'exact', path => $r };
516 }
517 }
518  
519 67 return \@rules;
520 }
521  
522 114451 ▶sub _is_ignored {
Subroutine _is_ignored: called CC=13 SCAR=25.7
523 114451 my ($path, $rules) = @_;
524 114451 ▶ return 0 unless $rules && @$rules;
Branch: 1 branch
BranchTrueFalseTotal
unless $rules and @$rules 114185 266 114451
Condition66%2/3
$rules and @$rules
!ll&&!rl&&r
0114185266
MC/DC50%1/2
$rules and @$rules
$rules @$rules
Truth table66%2/3
A and B
A= $rulesB= @$rules
ABresult
0X0
100
111
525  
526 # Split current path into parts
527 266 my @path_parts = grep { length $_ } split m{/}, $path;
528  
529 RULE:
530 266 for my $rule (@$rules) {
531  
532 305 ▶ if ($rule->{type} eq 'exact') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule->{'type'} eq 'exact') { } 219 86 305
elsif ($rule->{'type'} eq 'regex') { } 32 54 86
elsif ($rule->{'type'} eq 'wildcard') { } 54 0 54
533 219 ▶ return 1 if $path eq $rule->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $rule->{"path"} 43 176 219
534 }
535  
536 elsif ($rule->{type} eq 'regex') {
537 32 ▶ return 1 if $path =~ $rule->{re};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$rule->{"re"}/ 11 21 32
538 }
539  
540 elsif ($rule->{type} eq 'wildcard') {
541 54 my @r = @{ $rule->{parts} };
542  
543 54 ▶ next RULE unless @r == @path_parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @r == @path_parts 38 16 54
544  
545 16 for my $i (0 .. $#r) {
546 44 ▶ next if $r[$i] eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $r[$i] eq "*" 17 27 44
547 27 ▶ next RULE if $r[$i] ne $path_parts[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $r[$i] ne $path_parts[$i] 2 25 27
548 }
549  
550 14 return 1;
551 }
552 }
553  
554 198 return 0;
555 }
556  
557 =head1 LICENCE AND COPYRIGHT
558  
559 Copyright 2026 Nigel Horne.
560  
561 Usage is subject to licence terms.
562  
563 The licence terms of this software are as follows:
564  
565 =over 4
566  
567 =item * Personal single user, single computer use: GPL2
568  
569 =item * All other users (including Commercial, Charity, Educational, Government)
570 must apply in writing for a licence for use from Nigel Horne at the
571 above e-mail.
572  
573 =back
574  
575 =cut
576  
577 16 1;
↑ Summary Smart/Renderer/Test2.pm »