| 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
| ||||||||||||||||||||||||||||||||||
| 55 | 0 | croak "Too many items sent to compare"; | ||||||||||||||||||||||||||||||||
| 56 | } | |||||||||||||||||||||||||||||||||
| 57 | ||||||||||||||||||||||||||||||||||
| 58 | 65 | ▶ | return FALSE if defined $x xor defined $y; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
defined $x xor defined $y
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
| ||||||||||||||||||||||||||||||||||
| 59 | 63 | ▶ | return TRUE if ! defined $x and ! defined $y; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $x or defined $y
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
| ||||||||||||||||||||||||||||||||||
| 60 | ||||||||||||||||||||||||||||||||||
| 61 | 63 | ▶ | my $a = ref $x ? $x : \$x; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 62 | 63 | ▶ | my $b = ref $y ? $y : \$y; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||
| 68 | ||||||||||||||||||||||||||||||||||
| 69 | 62 | ▶ | if (ref $a eq 'SCALAR') { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||
| 75 | 19 | my @keys = keys %{$a}; | ||||||||||||||||||||||||||||||||
| 76 | 19 | my $max = scalar(@keys); | ||||||||||||||||||||||||||||||||
| 77 | 19 | ▶ | return FALSE if $max != scalar(keys %{$b}); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 78 | 18 | ▶ | return TRUE if $max == 0; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||
| 84 | } | |||||||||||||||||||||||||||||||||
| 85 | ||||||||||||||||||||||||||||||||||
| 86 | 17 | ▶ | return FALSE if $found != $max; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||
| 92 | } | |||||||||||||||||||||||||||||||||
| 93 | ||||||||||||||||||||||||||||||||||
| 94 | 10 | return TRUE; | ||||||||||||||||||||||||||||||||
| 95 | } | |||||||||||||||||||||||||||||||||
| 96 | ||||||||||||||||||||||||||||||||||
| 97 | 16 | ▶ | if (ref $a eq 'ARRAY') { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 98 | 16 | my $max = scalar(@{$a}); | ||||||||||||||||||||||||||||||||
| 99 | 16 | ▶ | return FALSE if $max != scalar(@{$b}); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 100 | 13 | ▶ | return TRUE if $max == 0; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 101 | ||||||||||||||||||||||||||||||||||
| 102 | 9 | ▶ | for (my $i = 0; $i < $max; ++$i) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 103 | # WARN: recursion may get really deep. | |||||||||||||||||||||||||||||||||
| 104 | 20 | ▶ | return FALSE unless compare($a->[$i], $b->[$i]); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 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__ | |||||||||||||||||||||||||||||||||