| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 33 | my $diff = _diff($value, $right->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 33 | ▶ | $out{$key} = $diff | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | if _has_diff($diff); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 20 | ▶ | return %out ? \%out : $no_diff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 134 | ▶ | return _diff_array($left, $right) if $ref_left eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 112 | ▶ | return _diff_hash($left, $right) if $ref_left eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 92 | ▶ | return _diff_scalar($left, $right) if $ref_left eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 83 | ▶ | return _diff_scalar($left, $right) if $ref_left eq 'REF'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 79 | ▶ | croak "cannot compare references to $ref_left" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | if $ref_left ne ''; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 79 | ▶ | return $left | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $left ne defined $right or defined $left and $left ne $right
defined $left and $left ne $right
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | return {} if $type eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | ▶ | return \undef if $type eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 23 | ▶ | $$out = $diff if ref $out; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 23 | return !!1; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 26 | ▶ | $$out = _empty_of_type($left) if ref $out; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 26 | return !!0; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # ABSTRACT: find out the difference between two Perl values | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||