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 (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