blib/lib/Value/Diff.pm

filter: Statementstmt 100% 69 / 69 Branchbran 97.5% 39 / 40 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 99.2% 130 / 131 SCAR 32.6
CC
26
Cov
99%
_diff
10.0
_diff_hash
5.0
_diff_array
5.0
CRAP 26.0
↑ Summary
1 package Value::Diff;
2 7 $Value::Diff::VERSION = '0.001';
3 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our @EXPORT = qw(diff);
11  
12 7 my $no_diff = \'no_diff';
13  
14 sub _has_diff
15 {
16 141 ▶ return !!1 unless defined $_[0];
Subroutine _has_diff: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 5 136 141
17 136 return $_[0] ne $no_diff;
18 }
19  
20 sub _diff_hash
21 {
22 20 ▶ my ($left, $right) = @_;
Subroutine _diff_hash: called CC=5 SCAR=16.1
23  
24 20 my %out;
25 20 for my $key (keys %{$left}) {
26 33 my $value = $left->{$key};
27 $out{$key} = $value
28 33 ▶ unless exists $right->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $right->{$key} 4 29 33
29  
30 33 my $diff = _diff($value, $right->{$key});
31 33 ▶ $out{$key} = $diff
Branch: 1 branch
BranchTrueFalseTotal
if _has_diff($diff) 8 25 33
32 if _has_diff($diff);
33 }
34  
35 20 ▶ return %out ? \%out : $no_diff;
Branch: 1 branch
BranchTrueFalseTotal
%out ? : 9 11 20
36 }
37  
38 sub _diff_array
39 {
40 22 ▶ my ($left, $right) = @_;
Subroutine _diff_array: called CC=5 SCAR=16.1
41  
42 22 my @out;
43 22 my @other = @{$right};
44  
45 OUTER:
46 22 for my $value (@{$left}) {
47 38 for my $key (0 .. $#other) {
48 46 my $other_value = $other[$key];
49 46 ▶ if (!_has_diff(_diff($value, $other_value))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_has_diff(_diff($value, $other_value))) 27 19 46
50 27 splice @other, $key, 1;
51 27 next OUTER;
52 }
53 }
54  
55 # TODO: take the smallest diff instead of full $left?
56 11 push @out, $value;
57 }
58  
59 22 ▶ return @out ? \@out : $no_diff;
Branch: 1 branch
BranchTrueFalseTotal
@out ? : 10 12 22
60 }
61  
62 sub _diff_scalar
63 {
64 13 ▶ my ($left, $right) = @_;
Subroutine _diff_scalar: called CC=2 SCAR=6.9
65  
66 13 my $diff = _diff($$left, $$right);
67 13 ▶ return _has_diff($diff) ? \$diff : $no_diff;
Branch: 1 branch
BranchTrueFalseTotal
_has_diff($diff) ? : 3 10 13
68 }
69  
70 sub _diff
71 {
72 141 ▶ my ($left, $right) = @_;
Subroutine _diff: called CC=10 SCAR=23.0
73  
74 141 my $ref_left = ref $left;
75 141 my $ref_right = ref $right;
76 141 ▶ return $left if $ref_left ne $ref_right;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_left ne $ref_right 7 134 141
77 134 ▶ return _diff_array($left, $right) if $ref_left eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref_left eq "ARRAY" 22 112 134
78 112 ▶ return _diff_hash($left, $right) if $ref_left eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref_left eq "HASH" 20 92 112
79 92 ▶ return _diff_scalar($left, $right) if $ref_left eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $ref_left eq "SCALAR" 9 83 92
80 83 ▶ return _diff_scalar($left, $right) if $ref_left eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if $ref_left eq "REF" 4 79 83
81  
82 79 ▶ croak "cannot compare references to $ref_left"
Branch: 1 branch
BranchTrueFalseTotal
if $ref_left ne "" 0 79 79
83 if $ref_left ne '';
84  
85 79 ▶ return $left
Branch: 1 branch
BranchTrueFalseTotal
if defined $left ne defined $right or defined $left and $left ne $right 24 55 79
Condition100%6/6
defined $left ne defined $right or defined $left and $left ne $right
l!l&&r!l&&!r
141154
defined $left and $left ne $right
!ll&&!rl&&r
104510
MC/DC100%3/3
defined $left ne defined $right or defined $left and $left ne $right
defined $left ne defined $right defined $left $left ne $right
Truth table100%4/4
A or B and C
A= defined $left ne defined $rightB= defined $leftC= $left ne $right
ABCresult
1XX1
0111
00X0
0100
86 if defined $left ne defined $right
87 || (defined $left && $left ne $right);
88  
89 55 return $no_diff;
90 }
91  
92 sub _empty_of_type
93 {
94 5 ▶ my ($left) = @_;
Subroutine _empty_of_type: called CC=4 SCAR=13.9
95  
96 5 my $type = ref $left;
97 5 ▶ return [] if $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 1 4 5
98 4 ▶ return {} if $type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 1 3 4
99 3 ▶ return \undef if $type eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" 1 2 3
100 2 return undef;
101 }
102  
103 sub diff
104 {
105 49 ▶ my ($left, $right, $out) = @_;
Subroutine diff: called CC=4 SCAR=13.9
Pod documented
106  
107 49 my $diff = _diff($left, $right);
108  
109 49 ▶ if (_has_diff($diff)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_has_diff($diff)) { } 23 26 49
110 23 ▶ $$out = $diff if ref $out;
Branch: 1 branch
BranchTrueFalseTotal
if ref $out 22 1 23
111 23 return !!1;
112 }
113 else {
114 26 ▶ $$out = _empty_of_type($left) if ref $out;
Branch: 1 branch
BranchTrueFalseTotal
if ref $out 5 21 26
115 26 return !!0;
116 }
117 }
118  
119 7 1;
120  
121 # ABSTRACT: find out the difference between two Perl values
122  
↑ Summary