Compare.pm

filter: Statementstmt 88.4% 153 / 173 Branchbran 81.4% 70 / 86 Conditioncond 68.6% 46 / 67 MC/DCmcdc 36.8% 14 / 38 Subroutinesub 94.1% 16 / 17 Podpod 33.3% 2 / 6 totaltotal 77.7% 301 / 387 CC 72 SCAR 33.6
CC
72
Cov
83%
_Compare
46.4
register_plugins
23.1
plugins_printable
20.0
CRAP 28.6
↑ Summary Compare/Plugins/Scalar/Properties.pm »
1 # Data::Compare - compare perl data structures
2 # Author: Fabien Tassin <fta@sofaraway.org>
3 # updated by David Cantrell <david@cantrell.org.uk>
4  
5 package Data::Compare;
6  
7 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use vars qw(@ISA @EXPORT $VERSION $DEBUG %been_there);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Scalar::Util qw(tainted);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 @ISA = qw(Exporter);
18 15 @EXPORT = qw(Compare);
19 15 $VERSION = 1.29;
20 15 ▶$DEBUG = $ENV{PERL_DATA_COMPARE_DEBUG} || 0;
Condition50%1/2
$ENV{'PERL_DATA_COMPARE_DEBUG'} || 0
l!l
015
MC/DC0%0/1
$ENV{'PERL_DATA_COMPARE_DEBUG'} || 0
$ENV{'PERL_DATA_COMPARE_DEBUG'}
Truth table50%1/2
A
A= $ENV{'PERL_DATA_COMPARE_DEBUG'}
Aresult
11
00
21  
22 15 my %handler;
23  
24 15 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 27 ▶sub import {
Subroutine import: called CC=3 SCAR=12.5
27 27 my $cwd = getcwd();
28 27 ▶ register_plugins() unless(tainted getcwd() || !chdir $cwd);
Branch: 1 branch
BranchTrueFalseTotal
unless &tainted(scalar getcwd()) or not chdir $cwd 27 0 27
Condition33%1/3
&tainted(scalar getcwd()) or not chdir $cwd
l!l&&r!l&&!r
0027
MC/DC0%0/2
&tainted(scalar getcwd()) or not chdir $cwd
&tainted(scalar getcwd()) not chdir $cwd
Truth table33%1/3
A or not(B)
A= &tainted(scalar getcwd())B= not chdir $cwd
ABresult
1X1
011
000
29 27 __PACKAGE__->export_to_level(1, @EXPORT);
30 }
31  
32 # finds and registers plugins
33 28 ▶sub register_plugins {
Subroutine register_plugins: called CC=14 SCAR=31.4
Pod undocumented
34 28 foreach my $file (
35 File::Find::Rule->file()->name('*.pm')->in(
36 88 map { "$_/Data/Compare/Plugins" }
37 236 grep { -d "$_/Data/Compare/Plugins" }
38 @INC
39 )
40 ) {
41 # all of this just to avoid loading the same plugin twice and
42 # generating a pile of warnings. Grargh!
43 88 $file =~ s!.*(Data/Compare/Plugins/.*)\.pm$!$1!;
44 88 $file =~ s!/!::!g;
45 # ignore badly named example from earlier version, oops
46 88 ▶ next if($file eq 'Data::Compare::Plugins::Scalar-Properties');
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "Data::Compare::Plugins::Scalar-Properties" 0 88 88
47 88 my $requires = eval "require $file";
48 88 ▶ next if($requires eq '1'); # already loaded this plugin?
Branch: 1 branch
BranchTrueFalseTotal
if $requires eq 1 74 14 88
49  
50 # not an arrayref? bail
51 14 ▶ if(ref($requires) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $requires ne "ARRAY") 0 14 14
52 0 warn("$file isn't a valid Data::Compare plugin (didn't return arrayref)\n");
53 0 return;
54 }
55 # coerce into arrayref of arrayrefs if necessary
56 0 ▶ if(ref((@{$requires})[0]) ne 'ARRAY') { $requires = [$requires] }
Branch: 1 branch
BranchTrueFalseTotal
if (ref((@{$requires;})[0]) ne "ARRAY") 0 14 14
57  
58 # register all the handlers
59 14 foreach my $require (@{$requires}) {
60 28 my($handler, $type1, $type2, $cruft) = reverse @{$require};
61 28 ▶ $type2 = $type1 unless(defined($type2));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type2 14 14 28
62 28 ($type1, $type2) = sort($type1, $type2);
63 28 ▶ if(!defined($type1) || ref($type1) ne '' || !defined($type2) || ref($type2) ne '') {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $type1 or ref $type1 ne '' or not defined $type2 or ref $type2 ne '') { } 0 28 28
elsif (defined $cruft) { } 0 28 28
elsif (ref $handler ne 'CODE') { } 0 28 28
Condition33%3/9
not defined $type1 or ref $type1 ne "" or not defined $type2 or ref $type2 ne ""
l!l&&r!l&&!r
0028
not defined $type1 or ref $type1 ne "" or not defined $type2
l!l&&r!l&&!r
0028
not defined $type1 or ref $type1 ne ""
l!l&&r!l&&!r
0028
MC/DC0%0/4
not defined $type1 or ref $type1 ne "" or not defined $type2 or ref $type2 ne ""
not defined $type1 ref $type1 ne "" not defined $type2 ref $type2 ne ""
Truth table20%1/5
not(A) or B or not(C) or D
A= not defined $type1B= ref $type1 ne ""C= not defined $type2D= ref $type2 ne ""
ABCDresult
1XXX1
01XX1
001X1
00011
00000
64 0 warn("$file isn't a valid Data::Compare plugin (invalid type)\n");
65 } elsif(defined($cruft)) {
66 0 warn("$file isn't a valid Data::Compare plugin (extra data)\n");
67 } elsif(ref($handler) ne 'CODE') {
68 0 warn("$file isn't a valid Data::Compare plugin (no coderef)\n");
69 } else {
70 28 $handler{$type1}{$type2} = $handler;
71 }
72 }
73 }
74 }
75  
76 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
77 3 my $this = shift;
78 3 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
79 3 my $self = {};
80 3 bless $self, $class;
81 3 $self->{'x'} = shift;
82 3 $self->{'y'} = shift;
83 3 return $self;
84 }
85  
86 7 ▶sub Cmp {
Subroutine Cmp: called CC=5 SCAR=16.2
Pod undocumented
87 7 my $self = shift;
88  
89 7 ▶ croak "Usage: DataCompareObj->Cmp(x, y)" unless $#_ == 1 || $#_ == -1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 1 or $#_ == -1 0 7 7
Condition66%2/3
$#_ == 1 or $#_ == -1
l!l&&r!l&&!r
520
MC/DC0%0/2
$#_ == 1 or $#_ == -1
$#_ == 1 $#_ == -1
Truth table66%2/3
A or B
A= $#_ == 1B= $#_ == -1
ABresult
1X1
011
000
90 7 ▶ my $x = shift || $self->{'x'};
Condition100%3/3
shift() || $self->{'x'}
l!l&&r!l&&!r
511
MC/DC100%2/2
shift() || $self->{'x'}
shift() $self->{'x'}
Truth table100%3/3
A || B
A= shift()B= $self->{'x'}
ABresult
1X1
011
000
91 7 ▶ my $y = shift || $self->{'y'};
Condition100%3/3
shift() || $self->{'y'}
l!l&&r!l&&!r
511
MC/DC100%2/2
shift() || $self->{'y'}
shift() $self->{'y'}
Truth table100%3/3
A || B
A= shift()B= $self->{'y'}
ABresult
1X1
011
000
92  
93 7 return Compare($x, $y);
94 }
95  
96 1073 ▶sub Compare {
Subroutine Compare: called CC=4 SCAR=14.0
Pod undocumented
97 1073 ▶ croak "Usage: Data::Compare::Compare(x, y, [opts])\n" unless $#_ == 1 || $#_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 1 or $#_ == 2 0 1073 1073
Condition66%2/3
$#_ == 1 or $#_ == 2
l!l&&r!l&&!r
106670
MC/DC0%0/2
$#_ == 1 or $#_ == 2
$#_ == 1 $#_ == 2
Truth table66%2/3
A or B
A= $#_ == 1B= $#_ == 2
ABresult
1X1
011
000
98  
99 1073 my $x = shift;
100 1073 my $y = shift;
101 1073 my $opts = {};
102 1073 ▶ if(@_) { $opts = clone(shift); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 1066 1073
103  
104 1073 _Compare($x, $y, $opts);
105 }
106  
107 2423 ▶sub _Compare {
Subroutine _Compare: called CC=44 SCAR=38.4
108 2423 my($x, $y, $opts) = @_;
109 my($xparent, $xpos, $yparent, $ypos) = map {
110 2423 ▶ $opts->{$_} || ''
Condition100%2/2
$opts->{$_} || ''
l!l
30606632
MC/DC100%1/1
$opts->{$_} || ''
$opts->{$_}
Truth table100%2/2
A
A= $opts->{$_}
Aresult
11
00
111 } qw(xparent xpos yparent ypos);
112  
113 2423 my $rval = '';
114  
115 2423 ▶ if(!exists($opts->{recursion_detector})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"recursion_detector"}) 1073 1350 2423
116 1073 %been_there = ();
117 1073 $opts->{recursion_detector} = 0;
118 }
119 2423 $opts->{recursion_detector}++;
120  
121 2423 ▶ warn "Yaroo! deep recursion!\n" if($opts->{recursion_detector} == 99);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"recursion_detector"} == 99 1 2422 2423
122  
123 2423 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x and exists $Data::Compare::been_there{"$x-$xpos-$xparent"} and $Data::Compare::been_there{"$x-$xpos-$xparent"} > 1 or ref $y and exists $Data::Compare::been_there{"$y-$ypos-$yparent"} and $Data::Compare::been_there{"$y-$ypos-$yparent"} > 1) { } 6 2417 2423
Condition86%13/15
ref $x and exists $Data::Compare::been_there{"$x-$xpos-$xparent"} and $Data::Compare::been_there{"$x-$xpos-$xparent"} > 1 or ref $y and exists $Data::Compare::been_there{"$y-$ypos-$yparent"} and $Data::Compare::been_there{"$y-$ypos-$yparent"} > 1
l!l&&r!l&&!r
602417
ref $x and exists $Data::Compare::been_there{"$x-$xpos-$xparent"} and $Data::Compare::been_there{"$x-$xpos-$xparent"} > 1
!ll&&!rl&&r
2403146
ref $x and exists $Data::Compare::been_there{"$x-$xpos-$xparent"}
!ll&&!rl&&r
1153125020
ref $y and exists $Data::Compare::been_there{"$y-$ypos-$yparent"} and $Data::Compare::been_there{"$y-$ypos-$yparent"} > 1
!ll&&!rl&&r
2403140
ref $y and exists $Data::Compare::been_there{"$y-$ypos-$yparent"}
!ll&&!rl&&r
1158124514
MC/DC50%3/6
ref $x and exists $Data::Compare::been_there{"$x-$xpos-$xparent"} and $Data::Compare::been_there{"$x-$xpos-$xparent"} > 1 or ref $y and exists $Data::Compare::been_there{"$y-$ypos-$yparent"} and $Data::Compare::been_there{"$y-$ypos-$yparent"} > 1
ref $x exists $Data::Compare::been_there{"$x-$xpos-$xparent"} $Data::Compare::been_there{"$x-$xpos-$xparent"} > 1 ref $y exists $Data::Compare::been_there{"$y-$ypos-$yparent"} $Data::Compare::been_there{"$y-$ypos-$yparent"} > 1
Truth table38%5/13
A and B and C or D and E and F
A= ref $xB= exists $Data::Compare::been_there{"$x-$xpos-$xparent"}C= $Data::Compare::been_there{"$x-$xpos-$xparent"} > 1D= ref $yE= exists $Data::Compare::been_there{"$y-$ypos-$yparent"}F= $Data::Compare::been_there{"$y-$ypos-$yparent"} > 1
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
124 (ref($x) && exists($been_there{"$x-$xpos-$xparent"}) && $been_there{"$x-$xpos-$xparent"} > 1) ||
125 (ref($y) && exists($been_there{"$y-$ypos-$yparent"}) && $been_there{"$y-$ypos-$yparent"} > 1)
126 ) {
127 6 $opts->{recursion_detector}--;
128 6 return 1; # we bail as soon as possible, so if we've *not* bailed and have got here, say we're OK and go to the next sub-structure
129 } else {
130 2417 ▶ $been_there{"$x-$xpos-$xparent"}++ if(ref($x));
Branch: 1 branch
BranchTrueFalseTotal
if ref $x 1264 1153 2417
131 2417 ▶ $been_there{"$y-$ypos-$yparent"}++ if(ref($y));
Branch: 1 branch
BranchTrueFalseTotal
if ref $y 1259 1158 2417
132  
133 $opts->{ignore_hash_keys} = { map {
134 4 ($_, 1)
135 2417 ▶ } @{$opts->{ignore_hash_keys}} } if(ref($opts->{ignore_hash_keys}) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"ignore_hash_keys"} eq "ARRAY" 4 2413 2417
136  
137 2417 my $refx = ref $x;
138 2417 my $refy = ref $y;
139  
140 2417 ▶ if(exists($handler{$refx}) && exists($handler{$refx}{$refy})) {
Branch: 11 branches
BranchTrueFalseTotal
if (exists $handler{$refx} and exists $handler{$refx}{$refy}) { } 0 2417 2417
elsif (exists $handler{$refy} and exists $handler{$refy}{$refx}) { } 0 2417 2417
elsif (not $refx || $refy) { } 1153 1264 2417
elsif ($refx ne $refy) { } 8 1256 1264
elsif (builtin::refaddr($x) == builtin::refaddr($y)) { } 14 1242 1256
elsif ($refx eq 'SCALAR' or $refx eq 'REF') { } 22 1220 1242
elsif ($refx eq 'ARRAY') { } 1127 93 1220
elsif ($refx eq 'HASH') { } 63 30 93
elsif ($refx eq 'Regexp') { } 2 28 30
elsif ($refx eq 'CODE') { } 0 28 28
elsif ($refx eq 'GLOB') { } 1 27 28
Condition66%2/3
exists $handler{$refx} and exists $handler{$refx}{$refy}
!ll&&!rl&&r
126411530
MC/DC0%0/2
exists $handler{$refx} and exists $handler{$refx}{$refy}
exists $handler{$refx} exists $handler{$refx}{$refy}
Truth table66%2/3
A and B
A= exists $handler{$refx}B= exists $handler{$refx}{$refy}
ABresult
0X0
100
111
141 0 $rval = &{$handler{$refx}{$refy}}($x, $y, $opts);
142 ▶ } elsif(exists($handler{$refy}) && exists($handler{$refy}{$refx})) {
Condition66%2/3
exists $handler{$refy} and exists $handler{$refy}{$refx}
!ll&&!rl&&r
125911580
MC/DC0%0/2
exists $handler{$refy} and exists $handler{$refy}{$refx}
exists $handler{$refy} exists $handler{$refy}{$refx}
Truth table66%2/3
A and B
A= exists $handler{$refy}B= exists $handler{$refy}{$refx}
ABresult
0X0
100
111
143 0 $rval = &{$handler{$refy}{$refx}}($x, $y, $opts);
144 }
145  
146 ▶ elsif(!$refx && !$refy) { # both are scalars
Condition66%2/3
$refx || $refy
l!l&&r!l&&!r
126401153
MC/DC50%1/2
$refx || $refy
$refx $refy
Truth table66%2/3
A || B
A= $refxB= $refy
ABresult
1X1
011
000
147 1153 ▶ if(defined $x && defined $y) { # both are defined
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x and defined $y) { } 1147 6 1153
Condition66%2/3
defined $x and defined $y
!ll&&!rl&&r
601147
MC/DC50%1/2
defined $x and defined $y
defined $x defined $y
Truth table66%2/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
148 1147 $rval = $x eq $y;
149 6 ▶ } else { $rval = !(defined $x || defined $y); }
Condition66%2/3
defined $x || defined $y
l!l&&r!l&&!r
024
MC/DC50%1/2
defined $x || defined $y
defined $x defined $y
Truth table66%2/3
A || B
A= defined $xB= defined $y
ABresult
1X1
011
000
150 }
151 elsif ($refx ne $refy) { # not the same type
152 8 $rval = 0;
153 }
154 elsif (Scalar::Util::refaddr($x) == Scalar::Util::refaddr($y)) { # exactly the same reference
155 14 $rval = 1;
156 }
157 ▶ elsif ($refx eq 'SCALAR' || $refx eq 'REF') {
Condition100%3/3
$refx eq "SCALAR" or $refx eq "REF"
l!l&&r!l&&!r
4181220
MC/DC100%2/2
$refx eq "SCALAR" or $refx eq "REF"
$refx eq "SCALAR" $refx eq "REF"
Truth table100%3/3
A or B
A= $refx eq "SCALAR"B= $refx eq "REF"
ABresult
1X1
011
000
158 22 $rval = _Compare(${$x}, ${$y}, $opts);
159 }
160 elsif ($refx eq 'ARRAY') {
161 1127 ▶ if ($#{$x} == $#{$y}) { # same length
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$x;} == $#{$y;}) { } 1124 3 1127
162 1124 my $i = -1;
163 1124 $rval = 1;
164 1124 for (@$x) {
165 1139 $i++;
166 1139 ▶ $rval = 0 unless _Compare($x->[$i], $y->[$i], { %{$opts}, xparent => $x, xpos => $i, yparent => $y, ypos => $i});
Branch: 1 branch
BranchTrueFalseTotal
unless _Compare($x->[$i], $y->[$i], {%{$opts;}, "xparent", $x, "xpos", $i, "yparent", $y, "ypos", $i}) 7 1132 1139
167 }
168 }
169 else {
170 3 $rval = 0;
171 }
172 }
173 elsif ($refx eq 'HASH') {
174 63 my @kx = grep { !$opts->{ignore_hash_keys}->{$_} } keys %$x;
175 63 my @ky = grep { !$opts->{ignore_hash_keys}->{$_} } keys %$y; # heh, KY
176 63 $rval = 1;
177 63 ▶ $rval = 0 unless scalar @kx == scalar @ky;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @kx == scalar @ky 7 56 63
178  
179 63 for (@kx) {
180 168 ▶ if(!exists($y->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $y->{$_}) 6 162 168
181 6 $rval = 0;
182 6 last;
183 }
184 162 ▶ $rval = 0 unless _Compare($x->{$_}, $y->{$_}, { %{$opts}, xparent => $x, xpos => $_, yparent => $y, ypos => $_});
Branch: 1 branch
BranchTrueFalseTotal
unless _Compare($x->{$_}, $y->{$_}, {%{$opts;}, "xparent", $x, "xpos", $_, "yparent", $y, "ypos", $_}) 7 155 162
185 }
186 }
187 elsif($refx eq 'Regexp') {
188 2 $rval = _Compare($x.'', $y.'', $opts);
189 }
190 elsif ($refx eq 'CODE') {
191 0 $rval = 0;
192 }
193 elsif ($refx eq 'GLOB') {
194 1 $rval = 0;
195 }
196 else { # a package name (object blessed)
197 27 my $type = Scalar::Util::reftype($x);
198 27 ▶ if ($type eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 21 6 27
elsif ($type eq 'ARRAY') { } 2 4 6
elsif ($type eq 'SCALAR' or $type eq 'REF') { } 2 2 4
elsif ($type eq 'GLOB') { } 1 1 2
elsif ($type eq 'CODE') { } 1 0 1
199 21 my %x = %$x;
200 21 my %y = %$y;
201 21 $rval = _Compare(\%x, \%y, { %{$opts}, xparent => $xparent, xpos => $xpos, yparent => $yparent, ypos => $ypos});
202 21 $been_there{\%x."-$xpos-$xparent"}--; # decrement count for temp structures
203 21 $been_there{\%y."-$ypos-$yparent"}--;
204 }
205 elsif ($type eq 'ARRAY') {
206 2 my @x = @$x;
207 2 my @y = @$y;
208 2 $rval = _Compare(\@x, \@y, { %{$opts}, xparent => $xparent, xpos => $xpos, yparent => $yparent, ypos => $ypos});
209 2 $been_there{\@x."-$xpos-$xparent"}--;
210 2 $been_there{\@y."-$ypos-$yparent"}--;
211 }
212 ▶ elsif ($type eq 'SCALAR' || $type eq 'REF') {
Condition66%2/3
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
202
MC/DC50%1/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table66%2/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
213 2 my $x = ${$x};
214 2 my $y = ${$y};
215 2 $rval = _Compare($x, $y, $opts);
216 # $been_there{\$x}--;
217 # $been_there{\$y}--;
218 }
219 elsif ($type eq 'GLOB') {
220 1 $rval = 0;
221 }
222 elsif ($type eq 'CODE') {
223 1 $rval = 0;
224 }
225 else {
226 0 croak "Can't handle $type type.";
227 0 $rval = 0;
228 }
229 }
230 }
231 2417 $opts->{recursion_detector}--;
232 2417 return $rval;
233 }
234  
235 3 ▶sub plugins {
Subroutine plugins: called CC=3 SCAR=11.0
Pod documented
236 3 ▶ return { map { (($_ eq '') ? '[scalar]' : $_, [map { $_ eq '' ? '[scalar]' : $_ } keys %{$handler{$_}}]) } keys %handler };
Branch: 2 branches
BranchTrueFalseTotal
$_ eq '' ? : 2 2 4
$_ eq '' ? : 0 4 4
237 }
238  
239 0 ▶sub plugins_printable {
Subroutine plugins_printable: not called CC=4 SCAR=30.0
Pod documented
240 0 my $r = "The following comparisons are available through plugins\n\n";
241 0 foreach my $key (sort keys %handler) {
242 0 foreach(sort keys %{$handler{$key}}) {
243 0 ▶ $r .= join(":\t", map { $_ eq '' ? '[scalar]' : $_ } ($key, $_))."\n";
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 0 0 0
244 }
245 }
246 0 return $r;
247 }
248  
249 15 1;
250  
251 =head1 NAME
252  
253 Data::Compare - compare perl data structures
254  
255 =head1 SYNOPSIS
256  
257 use Data::Compare;
258  
259 my $h1 = { 'foo' => [ 'bar', 'baz' ], 'FOO' => [ 'one', 'two' ] };
260 my $h2 = { 'foo' => [ 'bar', 'barf' ], 'FOO' => [ 'one', 'two' ] };
261 my @a1 = ('one', 'two');
262 my @a2 = ('bar', 'baz');
263 my %v = ( 'FOO', \@a1, 'foo', \@a2 );
264  
265 # simple procedural interface
266 print 'structures of $h1 and \%v are ',
267 Compare($h1, \%v) ? "" : "not ", "identical.\n";
268  
269 print 'structures of $h1 and $h2 are ',
270 Compare($h1, $h2, { ignore_hash_keys => [qw(foo)] }) ? '' : 'not ',
271 "close enough to identical.\n";
272  
273 # OO usage
274 my $c = new Data::Compare($h1, \%v);
275 print 'structures of $h1 and \%v are ',
276 $c->Cmp ? "" : "not ", "identical.\n";
277 # or
278 my $c = new Data::Compare;
279 print 'structures of $h and \%v are ',
280 $c->Cmp($h1, \%v) ? "" : "not ", "identical.\n";
281  
282 =head1 DESCRIPTION
283  
284 Compare two perl data structures recursively. Returns 0 if the
285 structures differ, else returns 1.
286  
287 A few data types are treated as special cases:
288  
289 =over 4
290  
291 =item Scalar::Properties objects
292  
293 This has been moved into a plugin, although functionality remains the
294 same as with the previous version. Full documentation is in
295 L<Data::Compare::Plugins::Scalar::Properties>.
296  
297 =item Compiled regular expressions, eg qr/foo/
298  
299 These are stringified before comparison, so the following will match:
300  
301 $r = qr/abc/i;
302 $s = qr/abc/i;
303 Compare($r, $s);
304  
305 and the following won't, despite them matching *exactly* the same text:
306  
307 $r = qr/abc/i;
308 $s = qr/[aA][bB][cC]/;
309 Compare($r, $s);
310  
311 Sorry, that's the best we can do.
312  
313 =item CODE and GLOB references
314  
315 These are assumed not to match unless the references are identical - ie,
316 both are references to the same thing.
317  
318 =back
319  
320 You may also customise how we compare structures by supplying options in
321 a hashref as a third parameter to the C<Compare()> function. This is not
322 yet available through the OO-ish interface. These options will be in
323 force for the *whole* of your comparison, so will apply to structures
324 that are lurking deep down in your data as well as at the top level, so
325 beware!
326  
327 =over 4
328  
329 =item ignore_hash_keys
330  
331 an arrayref of strings. When comparing two hashes, any keys mentioned in
332 this list will be ignored.
333  
334 =back
335  
336 =head1 CIRCULAR STRUCTURES
337  
338 Comparing a circular structure to itself returns true:
339  
340 $x = \$y;
341 $y = \$x;
342 Compare([$x, $y], [$x, $y]);
343  
344 And on a sort-of-related note, if you try to compare insanely deeply nested
345 structures, the module will spit a warning. For this to affect you, you need to go
346 around a hundred levels deep though, and if you do that you have bigger
347 problems which I can't help you with ;-)
348  
349 =head1 PLUGINS
350  
351 The module takes plug-ins so you can provide specialised routines for
352 comparing your own objects and data-types. For details see
353 L<Data::Compare::Plugins>.
354  
355 Plugins are *not* available when running in "taint" mode. You may
356 also make it not load plugins by providing an empty list as the
357 argument to import() - ie, by doing this:
358  
359 use Data::Compare ();
360  
361 A couple of functions are provided to examine what goodies have been
362 made available through plugins:
363  
364 =over 4
365  
366 =item plugins
367  
368 Returns a structure (a hash ref) describing all the comparisons made
369 available through plugins.
370 This function is *not* exported, so should be called as Data::Compare::plugins().
371 It takes no parameters.
372  
373 =item plugins_printable
374  
375 Returns formatted text
376  
377 =back
378  
379 =head1 EXPORTS
380  
381 For historical reasons, the Compare() function is exported. If you
382 don't want this, then pass an empty list to import() as explained
383 under PLUGINS. If you want no export but do want plugins, then pass
384 the empty list, and then call the register_plugins class method:
385  
386 use Data::Compare ();
387 Data::Compare->register_plugins;
388  
389 or you could call it as a function if that floats your boat.
390  
391 =head1 SOURCE CODE REPOSITORY
392  
393 L<git://github.com/DrHyde/perl-modules-Data-Compare.git>
394  
395 =head1 BUGS
396  
397 Plugin support is not quite finished (see the the Github
398 L<issue #5|http://github.com/DrHyde/perl-modules-Data-Compare/issues/5>
399 for details) but is usable. The missing bits are bells and whistles rather than
400 core functionality.
401  
402 Plugins are unavailable if you can't change to the current directory. This
403 might happen if you started your process as a priveleged user and then dropped
404 priveleges. If this affects you, please supply a portable patch with tests.
405  
406 Bug reports should be made on Github or by email.
407  
408 =head1 AUTHOR
409  
410 Fabien Tassin E<lt>fta@sofaraway.orgE<gt>
411  
412 Portions by David Cantrell E<lt>david@cantrell.org.ukE<gt>
413  
414 =head1 COPYRIGHT and LICENCE
415  
416 Copyright (c) 1999-2001 Fabien Tassin. All rights reserved.
417 This program is free software; you can redistribute it and/or
418 modify it under the same terms as Perl itself.
419  
420 Some parts copyright 2003 - 2023 David Cantrell.
421  
422 Seeing that Fabien seems to have disappeared, David Cantrell has become
423 a co-maintainer so he can apply needed patches. The licence, of course,
424 remains the same. As the "perl licence" is "Artistic or GPL, your choice",
425 you can find them as the files ARTISTIC.txt and GPL2.txt in the
426 distribution.
427  
428 =head1 SEE ALSO
429  
430 L<Test::Deep::NoTest>
431  
432 perl(1), perlref(1)
433  
434 =cut
↑ Summary Compare/Plugins/Scalar/Properties.pm »