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 struc‐
36 tures 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 struc‐
70 tures that are lurking deep down in your data as well as at the top
71 level, so beware!
72
73 ignore_hash_keys
74 an arrayref of strings. When comparing two hashes, any keys men‐
75 tioned 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 But comparing two different circular structures returns false:
85
86 $x = \$y;
87 $y = \$x;
88 Compare([$x, $y], [$y, $x]); # <-- note different order
89
90 And on a sort-of-related note, if you try to compare insanely deeply
91 nested structures, the module will spit a warning. For this to affect
92 you, you need to go around a hundred levels deep though, and if you do
93 that you have bigger problems which I can't help you with ;-)
94
96 The module takes plug-ins so you can provide specialised routines for
97 comparing your own objects and data-types. For details see Data::Com‐
98 pare::Plugins.
99
100 Plugins are *not* available when running in "taint" mode.
101
102 A couple of functions are provided to examine what goodies have been
103 made available through plugins:
104
105 plugins
106 Returns a structure (a hash ref) describing all the comparisons
107 made available through plugins. This function is *not* exported,
108 so should be called as Data::Compare::plugins(). It takes no
109 parameters.
110
111 plugins_printable
112 Returns formatted text
113
115 Plugin support is not quite finished (see the TODO file for details)
116 but is usable. The missing bits are bells and whistles rather than
117 core functionality.
118
119 Plugins are unavailable if you can't change to the current directory.
120 This might happen if you started your process as a priveleged user and
121 then dropped priveleges. This is due to how we check for Taintedness.
122 If this affects you, please supply a portable patch.
123
125 Fabien Tassin fta@sofaraway.org
126
127 Copyright (c) 1999-2001 Fabien Tassin. All rights reserved. This pro‐
128 gram is free software; you can redistribute it and/or modify it under
129 the same terms as Perl itself.
130
131 Seeing that Fabien seems to have disappeared, David Cantrell has become
132 a co-maintainer so he can apply needed patches. The licence, of
133 course, remains the same, and all communications about this module
134 should be CCed to Fabien in case he ever returns and wants his baby
135 back.
136
137 Portions, including plugins, copyright 2003-2007 David Cantrell
138 david@cantrell.org.uk
139
141 perl(1), perlref(1)
142
143
144
145perl v5.8.8 2004-01-07 Data::Compare(3)