blib/lib/Struct/Compare.pm

filter: Statementstmt 96.7% 59 / 61 Branchbran 86.1% 31 / 36 Conditioncond 57.1% 4 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 89.6% 104 / 116 CC 23 SCAR 29.6
CC
23
Cov
90%
compare
24.0
BEGIN
1.0
BEGIN
1.0
CRAP 19.2
↑ Summary
1 package Struct::Compare;
2  
3 =head1 NAME
4  
5 Struct::Compare - Recursive diff for perl structures.
6  
7 =head1 SYNOPSIS
8  
9 use Struct::Compare;
10 my $is_different = compare($ref1, $ref2);
11  
12 =head1 DESCRIPTION
13  
14 Compares two values of any type and structure and returns true if they
15 are the same. It does a deep comparison of the structures, so a hash
16 of a hash of a whatever will be compared correctly.
17  
18 This is especially useful for writing unit tests for your modules!
19  
20 =head1 PUBLIC FUNCTIONS
21  
22 =over 4
23  
24 =cut
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 require Exporter;
28 1 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 @ISA = qw(Exporter);
32 1 @EXPORT = qw(compare);
33  
34 1 $VERSION = '1.0.1';
35  
36 # TODO: document
37  
38 1 ▶use constant FALSE=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant TRUE =>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant DEBUG=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =item * $bool = compare($var1, $var2)
43  
44 Recursively compares $var1 to $var2, returning false if either
45 structure is different than the other at any point. If both are
46 undefined, it returns true as well, because that is considered equal.
47  
48 =cut
49  
50 65 ▶sub compare {
Subroutine compare: called CC=23 SCAR=31.8
Pod documented
51 65 my $x = shift;
52 65 my $y = shift;
53  
54 65 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 65 65
55 0 croak "Too many items sent to compare";
56 }
57  
58 65 ▶ return FALSE if defined $x xor defined $y;
Branch: 1 branch
BranchTrueFalseTotal
if defined $x xor defined $y 2 63 65
Condition75%3/4
defined $x xor defined $y
l&&rl&&!r!l&&r!l&&!r
63110
MC/DC100%2/2
defined $x xor defined $y
defined $x defined $y
Truth table75%3/4
A xor B
A= defined $xB= defined $y
ABresult
110
101
011
000
59 63 ▶ return TRUE if ! defined $x and ! defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x or defined $y 0 63 63
Condition33%1/3
defined $x or defined $y
l!l&&r!l&&!r
6300
MC/DC0%0/2
defined $x or defined $y
defined $x defined $y
Truth table33%1/3
A or B
A= defined $xB= defined $y
ABresult
1X1
011
000
60  
61 63 ▶ my $a = ref $x ? $x : \$x;
Branch: 1 branch
BranchTrueFalseTotal
ref $x ? : 36 27 63
62 63 ▶ my $b = ref $y ? $y : \$y;
Branch: 1 branch
BranchTrueFalseTotal
ref $y ? : 36 27 63
63  
64 63 print "\$a is a ", ref $a, "\n" if DEBUG;
65 63 print "\$b is a ", ref $b, "\n" if DEBUG;
66  
67 63 ▶ return FALSE unless ref $a eq ref $b;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $a eq ref $b 1 62 63
68  
69 62 ▶ if (ref $a eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "SCALAR") 27 35 62
70 27 print "a = $$a, b = $$b\n" if DEBUG;
71 27 return $$a eq $$b;
72 }
73  
74 35 ▶ if (ref $a eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "HASH") 19 16 35
75 19 my @keys = keys %{$a};
76 19 my $max = scalar(@keys);
77 19 ▶ return FALSE if $max != scalar(keys %{$b});
Branch: 1 branch
BranchTrueFalseTotal
if $max != scalar keys %{$b;} 1 18 19
78 18 ▶ return TRUE if $max == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max == 0 1 17 18
79  
80 # first just look to see if there are any keys not in the other;
81 17 my $found = 0;
82 17 foreach my $key (@keys) {
83 27 ▶ $found++ if exists $b->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $b->{$key} 27 0 27
84 }
85  
86 17 ▶ return FALSE if $found != $max;
Branch: 1 branch
BranchTrueFalseTotal
if $found != $max 0 17 17
87  
88 # now compare the values
89 17 foreach my $key (@keys) {
90 # WARN: recursion may get really deep.
91 23 ▶ return FALSE unless compare($a->{$key}, $b->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless compare($a->{$key}, $b->{$key}) 7 16 23
92 }
93  
94 10 return TRUE;
95 }
96  
97 16 ▶ if (ref $a eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "ARRAY") 16 0 16
98 16 my $max = scalar(@{$a});
99 16 ▶ return FALSE if $max != scalar(@{$b});
Branch: 1 branch
BranchTrueFalseTotal
if $max != scalar @{$b;} 3 13 16
100 13 ▶ return TRUE if $max == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max == 0 4 9 13
101  
102 9 ▶ for (my $i = 0; $i < $max; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 20 7 27
103 # WARN: recursion may get really deep.
104 20 ▶ return FALSE unless compare($a->[$i], $b->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless compare($a->[$i], $b->[$i]) 2 18 20
105 }
106  
107 7 return TRUE;
108 }
109  
110 # FIX: doesn't deal with non-basic types... see if you can fake it.
111  
112 0 return FALSE;
113 }
114  
115 1 1;
116  
117 __END__
↑ Summary