1Data::Compare(3) User Contributed Perl Documentation Data::Compare(3)
2
3
4
6 Data::Compare - compare perl data structures
7
9 use Data::Compare;
10
11 my $h1 = { 'foo' => [ 'bar', 'baz' ], 'FOO' => [ 'one', 'two' ] };
12 my $h2 = { 'foo' => [ 'bar', 'barf' ], 'FOO' => [ 'one', 'two' ] };
13 my @a1 = ('one', 'two');
14 my @a2 = ('bar', 'baz');
15 my %v = ( 'FOO', \@a1, 'foo', \@a2 );
16
17 # simple procedural interface
18 print 'structures of $h1 and \%v are ',
19 Compare($h1, \%v) ? "" : "not ", "identical.\n";
20
21 print 'structures of $h1 and $h2 are ',
22 Compare($h1, $h2, { ignore_hash_keys => [qw(foo)] }) ? '' : 'not ',
23 "close enough to identical.\n";
24
25 # OO usage
26 my $c = new Data::Compare($h1, \%v);
27 print 'structures of $h1 and \%v are ',
28 $c->Cmp ? "" : "not ", "identical.\n";
29 # or
30 my $c = new Data::Compare;
31 print 'structures of $h and \%v are ',
32 $c->Cmp($h1, \%v) ? "" : "not ", "identical.\n";
33
35 Compare two perl data structures recursively. Returns 0 if the
36 structures differ, else returns 1.
37
38 A few data types are treated as special cases:
39
40 Scalar::Properties objects
41 This has been moved into a plugin, although functionality remains
42 the same as with the previous version. Full documentation is in
43 Data::Compare::Plugins::Scalar::Properties.
44
45 Compiled regular expressions, eg qr/foo/
46 These are stringified before comparison, so the following will
47 match:
48
49 $r = qr/abc/i;
50 $s = qr/abc/i;
51 Compare($r, $s);
52
53 and the following won't, despite them matching *exactly* the same
54 text:
55
56 $r = qr/abc/i;
57 $s = qr/[aA][bB][cC]/;
58 Compare($r, $s);
59
60 Sorry, that's the best we can do.
61
62 CODE and GLOB references
63 These are assumed not to match unless the references are identical
64 - ie, both are references to the same thing.
65
66 You may also customise how we compare structures by supplying options
67 in a hashref as a third parameter to the "Compare()" function. This is
68 not yet available through the OO-ish interface. These options will be
69 in force for the *whole* of your comparison, so will apply to
70 structures that are lurking deep down in your data as well as at the
71 top level, so beware!
72
73 ignore_hash_keys
74 an arrayref of strings. When comparing two hashes, any keys
75 mentioned in this list will be ignored.
76
78 Comparing a circular structure to itself returns true:
79
80 $x = \$y;
81 $y = \$x;
82 Compare([$x, $y], [$x, $y]);
83
84 And on a sort-of-related note, if you try to compare insanely deeply
85 nested structures, the module will spit a warning. For this to affect
86 you, you need to go around a hundred levels deep though, and if you do
87 that you have bigger problems which I can't help you with ;-)
88
90 The module takes plug-ins so you can provide specialised routines for
91 comparing your own objects and data-types. For details see
92 Data::Compare::Plugins.
93
94 Plugins are *not* available when running in "taint" mode. You may also
95 make it not load plugins by providing an empty list as the argument to
96 import() - ie, by doing this:
97
98 use Data::Compare ();
99
100 A couple of functions are provided to examine what goodies have been
101 made available through plugins:
102
103 plugins
104 Returns a structure (a hash ref) describing all the comparisons
105 made available through plugins. This function is *not* exported,
106 so should be called as Data::Compare::plugins(). It takes no
107 parameters.
108
109 plugins_printable
110 Returns formatted text
111
113 For historical reasons, the Compare() function is exported. If you
114 don't want this, then pass an empty list to import() as explained under
115 PLUGINS. If you want no export but do want plugins, then pass the
116 empty list, and then call the register_plugins class method:
117
118 use Data::Compare ();
119 Data::Compare->register_plugins;
120
121 or you could call it as a function if that floats your boat.
122
124 <git://github.com/DrHyde/perl-modules-Data-Compare.git>
125
127 Plugin support is not quite finished (see the TODO file for details)
128 but is usable. The missing bits are bells and whistles rather than
129 core functionality.
130
131 Please report any other bugs either by email to David Cantrell (see
132 below for address) or using rt.cpan.org:
133
134 <https://rt.cpan.org/Ticket/Create.html?Queue=Data-Compare>
135
137 Fabien Tassin <fta@sofaraway.org>
138
139 Portions by David Cantrell <david@cantrell.org.uk>
140
142 Copyright (c) 1999-2001 Fabien Tassin. All rights reserved. This
143 program is free software; you can redistribute it and/or modify it
144 under the same terms as Perl itself.
145
146 Some parts copyright 2003 - 2014 David Cantrell.
147
148 Seeing that Fabien seems to have disappeared, David Cantrell has become
149 a co-maintainer so he can apply needed patches. The licence, of
150 course, remains the same. As the "perl licence" is "Artistic or GPL,
151 your choice", you can find them as the files ARTISTIC.txt and GPL2.txt
152 in the distribution.
153
155 Test::Deep::NoTest
156
157 perl(1), perlref(1)
158
159
160
161perl v5.28.0 2014-12-10 Data::Compare(3)