Data::Compare(3)
π Data::Compare π
okay so bestie, welcome to Data::Compare β the perl module that's literally the vibe check for your data structures β¨
πΈ NAME πΈ
Data::Compare β compare perl data structures, no cap π
β¨ SYNOPSIS β¨
like, here's the whole mood, fr fr:
use Data::Compare;
my $h1 = { 'foo' => [ 'bar', 'baz' ], 'FOO' => [ 'one', 'two' ] };
my $h2 = { 'foo' => [ 'bar', 'barf' ], 'FOO' => [ 'one', 'two' ] };
my @a1 = ('one', 'two');
my @a2 = ('bar', 'baz');
my %v = ( 'FOO', \@a1, 'foo', \@a2 );
# simple procedural interface
print 'structures of $h1 and \%v are ',
Compare($h1, \%v) ? "" : "not ", "identical.\n";
print 'structures of $h1 and $h2 are ',
Compare($h1, $h2, { ignore_hash_keys => [qw(foo)] }) ? '' : 'not ',
"close enough to identical.\n";
# OO usage
my $c = new Data::Compare($h1, \%v);
print 'structures of $h1 and \%v are ',
$c->Cmp ? "" : "not ", "identical.\n";
# or
my $c = new Data::Compare;
print 'structures of $h and \%v are ',
$c->Cmp($h1, \%v) ? "" : "not ", "identical.\n";
periodt π
π DESCRIPTION π
okay so the whole point: it compares two perl data structures recursively, like it goes all the way down into every little nested thing. Returns 0 if they're different, otherwise returns 1. simple as that, bestie π
now, a few data types get treated as β¨special casesβ¨ because the universe is chaotic:
π¦ Scalar::Properties objects π¦
this got moved into a plugin, but it still works exactly the same as before, no cap. full docs live in Data::Compare::Plugins::Scalar::Properties if you wanna read up π
π Compiled regular expressions, eg qr/foo/ π
these get stringified before comparison, so like⦠this one will match:
$r = qr/abc/i;
$s = qr/abc/i;
Compare($r, $s);
but this one won't, even though they literally match the exact same text π:
$r = qr/abc/i;
$s = qr/[aA][bB][cC]/;
Compare($r, $s);
sorry bestie, that's the best we can do, i don't make the rules π
π CODE and GLOB references π
these are assumed not to match unless the references are literally identical β like, both are references to the same thing. no vibes-based matching here, sorry π«Ά
π customising your comparison π
you can also customise how we compare stuff by passing options in a hashref as the third parameter to the Compare() function. lowkey important: this is not yet available through the OO-ish interface π
β οΈ these options are in force for the whole of your comparison, so they apply to structures lurking deep down in your data as well as at the top level β beware, bestie, it's giving main character energy that affects everything π
π ignore_hash_keys π
an arrayref of strings. when comparing two hashes, any keys mentioned in this list get ignored. iconic for when you don't care about certain fields β¨
π CIRCULAR STRUCTURES π
comparing a circular structure to itself returns true:
$x = \$y;
$y = \$x;
Compare([$x, $y], [$x, $y]);
and on a sort-of-related note (iykyk), if you try to compare insanely deeply nested structures, the module will spit a warning. for this to actually affect you though, you need to go like a hundred levels deep β and honestly if you're doing that, you have bigger problems which i can't help you with ;-) π
π§ PLUGINS π§
the module takes plug-ins so you can provide specialised routines for comparing your own objects and data-types. for deets see Data::Compare::Plugins π
π plugins are not available when running in "taint" mode.
you can also make it not load plugins by providing an empty list as the argument to import() β ie, by doing this:
use Data::Compare ();
a couple of functions are provided to examine what goodies have been made available through plugins:
πΈ plugins πΈ
returns a structure (a hash ref) describing all the comparisons made available through plugins. this function is not exported, so should be called as Data::Compare::plugins(). it takes no parameters. slay π
πΈ plugins_printable πΈ
returns formatted text β¨
π EXPORTS π
for historical reasons, the Compare() function is exported. if you don't want this, then pass an empty list to import() as explained under PLUGINS. if you want no export but do want plugins, then pass the empty list, and then call the register_plugins class method:
use Data::Compare ();
Data::Compare->register_plugins;
or you could call it as a function if that floats your boat πΆ
π¦ SOURCE CODE REPOSITORY π¦
<git://github.com/DrHyde/perl-modules-Data-Compare.git> β go look, bestie π
π BUGS π
plugin support is not quite finished (see the the Github issue #5 <http://github.com/DrHyde/perl-modules-Data-Compare/issues/5> for details) but is usable. the missing bits are bells and whistles rather than core functionality, so likeβ¦ it works, don't stress π
plugins are unavailable if you can't change to the current directory. this might happen if you started your process as a priveleged user and then dropped priveleges. if this affects you, please supply a portable patch with tests π«Ά
bug reports should be made on Github or by email β¨
β AUTHOR β
Fabien Tassin <fta@sofaraway.org> π
Portions by David Cantrell <david@cantrell.org.uk> π
π COPYRIGHT and LICENCE π
Copyright (c) 1999-2001 Fabien Tassin. All rights reserved. this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself π
Some parts copyright 2003 - 2023 David Cantrell.
seeing that Fabien seems to have disappeared, David Cantrell has become a co-maintainer so he can apply needed patches. the licence, of course, remains the same. as the "perl licence" is "Artistic or GPL, your choice", you can find them as the files ARTISTIC.txt and GPL2.txt in the distribution π
π SEE ALSO π
Test::Deep::NoTest
perl(1), perlref(1) π
and that's the whole manpage bestie! this was perl v5.42.0, dated 2023-04-22 β go forth and compare your structures with confidence, you absolute icon π
β¨π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-Data-Compare 1.29-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms