lib/Sub/IsEqual.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 95.0% 19 / 20 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 95.5% 85 / 89 CC 15 SCAR 23.2
CC
15
Cov
96%
is_equal
15.0
BEGIN
1.0
BEGIN
1.0
CRAP 10.2
↑ Summary
1 package Sub::IsEqual;
2  
3 =head1 NAME
4  
5 Sub::IsEqual - determine if two arguments are equal
6  
7 =cut
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Exporter qw{import};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::Util qw{first};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw{refaddr};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Set::Functional qw{symmetric_difference};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 VERSION
18  
19 Version 0.03
20  
21 =cut
22  
23 1 our $VERSION = '0.03';
24  
25 =head1 SYNOPSIS
26  
27 This module provides a function called is_equal to determine if any two
28 arbitrary arguments are the same. Equality is determined by definedness,
29 structure, and string equality, so 1 and 1.0 will be considered inequal.
30 For data structures, circular references will be detected.
31  
32 =cut
33  
34 =head1 METHODS
35  
36 =cut
37  
38 1 our @EXPORT_OK = qw{is_equal};
39  
40 =head2 is_equal
41  
42 Given 2 arguments, determine if they are equivalent using string equality
43 and deep comparison. For large data structures, is_equal will attempt to
44 walk the structure, comparing all key-value paris for hashes, checking the
45 order in arrays, and following all references while checking for loops.
46 Blessed objects must be the same value in memory, by default, but may define
47 their own equivalence by overloading the eq operator. The only exception
48 to all of this is undef, which is only equivalent to itself.
49  
50 Examples:
51  
52 is_equal(undef, undef); # => true
53 is_equal(undef, ''); # => false
54 is_equal(1, 1.0); # => false
55 is_equal("mom", "mom"); # => true
56 is_equal([qw{hello world}], [qw{hello world}]); # => true
57 is_equal({hello => 1}, {hello => 1}); # => true
58  
59 =cut
60  
61 74 ▶sub is_equal {
Subroutine is_equal: called CC=15 SCAR=27.1
Pod documented
62 74 my ($left, $right, $recursion_check) = @_;
63  
64 #Check that both values are in the same state of definedness
65 74 ▶ return 0 if defined($left) ^ defined($right);
Branch: 1 branch
BranchTrueFalseTotal
if defined $left ^ defined $right 2 72 74
66 #Check that both values are defined
67 72 ▶ return 1 if ! defined($left);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left 1 71 72
68 #Check that both values are string equivalent
69 71 ▶ return 1 if $left eq $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left eq $right 38 33 71
70  
71 33 my ($left_ref, $right_ref) = (ref($left), ref($right));
72  
73 #Check that both values refer to the same type of thing
74 33 ▶ return 0 if $left_ref ne $right_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $left_ref ne $right_ref 3 30 33
75 #Check that both values are references
76 30 ▶ return 0 if $left_ref eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $left_ref eq "" 5 25 30
77  
78 25 ▶ $recursion_check ||= {};
Condition100%2/2
$recursion_check ||= {}
l!l
817
MC/DC100%1/1
$recursion_check ||= {}
$recursion_check
Truth table100%2/2
A
A= $recursion_check
Aresult
11
00
79 25 my ($left_refaddr, $right_refaddr) = (refaddr($left), refaddr($right));
80  
81 #Check that both references are in the same visit state
82 25 ▶ return 0 if exists $recursion_check->{$left_refaddr} ^ exists $recursion_check->{$right_refaddr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $recursion_check->{$left_refaddr} ^ exists $recursion_check->{$right_refaddr} 1 24 25
83 #Check that both references have already been visited
84 24 ▶ return 1 if exists $recursion_check->{$left_refaddr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $recursion_check->{$left_refaddr} 4 20 24
85  
86 20 undef $recursion_check->{$left_refaddr};
87 20 undef $recursion_check->{$right_refaddr};
88  
89 #Check that scalar references point to the same values
90 20 ▶ if ($left_ref eq 'SCALAR' || $left_ref eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if ($left_ref eq 'SCALAR' or $left_ref eq 'REF') { } 2 18 20
elsif ($left_ref eq 'ARRAY') { } 13 5 18
elsif ($left_ref eq 'HASH') { } 5 0 5
Condition66%2/3
$left_ref eq "SCALAR" or $left_ref eq "REF"
l!l&&r!l&&!r
2018
MC/DC50%1/2
$left_ref eq "SCALAR" or $left_ref eq "REF"
$left_ref eq "SCALAR" $left_ref eq "REF"
Truth table66%2/3
A or B
A= $left_ref eq "SCALAR"B= $left_ref eq "REF"
ABresult
1X1
011
000
91 2 return is_equal($$left, $$right, $recursion_check);
92  
93 #Check that arrays have the same values in the same order
94 } elsif ($left_ref eq 'ARRAY') {
95 return
96 @$left == @$right
97 13 ▶ && ! defined(first { ! is_equal($left->[$_], $right->[$_], $recursion_check) } (0 .. $#$left));
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
@$left == @$right && !defined(&first(sub { not is_equal($left->[$_], $right->[$_], $recursion_check); } , 0 .. $left->$#*))
!ll&&!rl&&r
139
MC/DC100%2/2
@$left == @$right && !defined(&first(sub { not is_equal($left->[$_], $right->[$_], $recursion_check); } , 0 .. $left->$#*))
@$left == @$right !defined(&first(sub { not is_equal($left->[$_], $right->[$_], $recursion_check); } , 0 .. $left->$#*))
Truth table100%3/3
A && not(B)
A= @$left == @$rightB= !defined(&first(sub { not is_equal($left->[$_], $right->[$_], $recursion_check); } , 0 .. $left->$#*))
ABresult
0X0
100
111
98  
99 #Check that hashes contain the same keys pointing to the same values
100 } elsif ($left_ref eq 'HASH') {
101 return
102 ! symmetric_difference([keys %$left], [keys %$right])
103 5 ▶ && ! defined(first { ! is_equal($left->{$_}, $right->{$_}, $recursion_check) } keys %$left);
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
&symmetric_difference([keys %$left], [keys %$right]) || defined &first(sub { not is_equal($left->{$_}, $right->{$_}, $recursion_check); } , keys %$left)
l!l&&r!l&&!r
113
MC/DC100%2/2
&symmetric_difference([keys %$left], [keys %$right]) || defined &first(sub { not is_equal($left->{$_}, $right->{$_}, $recursion_check); } , keys %$left)
&symmetric_difference([keys %$left], [keys %$right]) defined &first(sub { not is_equal($left->{$_}, $right->{$_}, $recursion_check); } , keys %$left)
Truth table100%3/3
A || B
A= &symmetric_difference([keys %$left], [keys %$right])B= defined &first(sub { not is_equal($left->{$_}, $right->{$_}, $recursion_check); } , keys %$left)
ABresult
1X1
011
000
104  
105 #Give up
106 } else {
107 0 die "Must define string equality for type [$left_ref]";
108 }
109 }
110  
111 =head1 AUTHOR
112  
113 Aaron Cohen, C<< <aarondcohen at gmail.com> >>
114  
115 =head1 ACKNOWLEDGEMENTS
116  
117 This module was made possible by L<Shutterstock|http://www.shutterstock.com/>
118 (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source
119 projects from Shutterstock can be found at
120 L<code.shutterstock.com|http://code.shutterstock.com/>.
121  
122 =head1 BUGS
123  
124 Please report any bugs or feature requests to C<bug-sub-isequal at rt.cpan.org>, or through
125 the web interface at L<https://github.com/aarondcohen/perl-sub-isequal/issues>. I will
126 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
127  
128 =head1 SUPPORT
129  
130 You can find documentation for this module with the perldoc command.
131  
132 perldoc Sub::IsEqual
133  
134 You can also look for information at:
135  
136 =over 4
137  
138 =item * Official GitHub Repo
139  
140 L<https://github.com/aarondcohen/perl-sub-isequal>
141  
142 =item * GitHub's Issue Tracker (report bugs here)
143  
144 L<https://github.com/aarondcohen/perl-sub-isequal/issues>
145  
146 =item * CPAN Ratings
147  
148 L<http://cpanratings.perl.org/d/Sub-IsEqual>
149  
150 =item * Official CPAN Page
151  
152 L<http://search.cpan.org/dist/Sub-IsEqual/>
153  
154 =back
155  
156 =head1 LICENSE AND COPYRIGHT
157  
158 Copyright 2013 Aaron Cohen.
159  
160 This program is free software; you can redistribute it and/or modify it
161 under the terms of either: the GNU General Public License as published
162 by the Free Software Foundation; or the Artistic License.
163  
164 See http://dev.perl.org/licenses/ for more information.
165  
166 =cut
167  
168  
169 1 1;
↑ Summary