Test2::Compare(3pm)
π Test2::Compare π
Okay bestie, so this page is the Test2::Compare(3pm) manual β that's Test2::Compare in section 3pm of the Perl manual, iykyk β and it's genuinely iconic because it's the driving force behind deep comparison tools such as Test2::Tools::Compare::is() and Test2::Tools::ClassicCompare::is_deeply(). Like, those functions are the main characters, and this library is the whole backstage crew making it happen. No cap.
β¨ DESCRIPTION β¨
This library is the driving force behind deep comparison tools such as Test2::Tools::Compare::is() and Test2::Tools::ClassicCompare::is_deeply().
That's it. That's the whole thing. Everything else on this page is just the receipts. π₯
π¦ SYNOPSIS π¦
Like, here's the vibe in code form:
package Test2::Tools::MyCheck;
use Test2::Compare::MyCheck;
use Test2::Compare qw/compare/;
sub MyCheck {
my ($got, $exp, $name, @diag) = @_;
my $ctx = context();
my $delta = compare($got, $exp, \&convert);
if ($delta) {
$ctx->fail($name, $delta->diag, @diag);
}
else {
$ctx->ok(1, $name);
}
$ctx->release;
return !$delta;
}
sub convert {
my $thing = shift;
return $thing if blessed($thing) && $thing->isa('Test2::Compare::MyCheck');
return Test2::Compare::MyCheck->new(stuff => $thing);
}
And if you actually wanna learn how to write your own custom check, go read Test2::Compare::Base for details β it's got the full lore. π
π EXPORTS π
These are the goodies that come out of the box, bestie:
$delta = compare($got, $expect, \&convert)β This will compare the structures in$gotwith those in$expect. The convert sub should convert vanilla structures inside$expectinto checks. If there are differences in the structures they will be reported back as anTest2::Compare::Deltatree. Yup, a whole tree. It's giving family reunion. π³$build = get_build()β Get the current global build, if any. (If there isn't one, honestly, that's on you.)push_build($build)β Set the current global build.$build = pop_build($build)β Unset the current global build. Heads up though: this will throw an exception if the build passed in is different from the current global. So don't be messy about it. πbuild($class, sub { ... })β Run the provided codeblock with a new instance of$classas the current build. Returns the new build. Slay.$check = convert($thing)$check = convert($thing, $config)β This convert function is used bystrict_convert()andrelaxed_convert()under the hood. It can also be used as the basis for other convert functions.
If you want to use it with a custom configuration you should wrap it in another sub like so:
sub my_convert {
my $thing_to_convert = shift;
return convert(
$thing_to_convert,
{ ... }
);
}
Or the short variant:
sub my_convert { convert($_[0], { ... }) }
There are several configuration options β here they are, with the default setting listed first:
implicit_end => 1β This option toggles array/hash boundaries. If this is true then no extra hash keys or array indexes will be allowed. This setting affects generated compare objects as well as any passed in. So like, the boundaries are boundaried. π§use_regex => 1β This option toggles regex matching. When true (default) regexes are converted to checks such that values must match the regex. When false regexes will be compared to see if they are identical regexes. Very demure, very mindful.use_code => 0β This option toggles code matching. When false (default) coderefs in structures must be the same coderef as specified. When true coderefs will be run to verify the value being checked. Yes, it literally runs the code. Respect. π₯
$check = strict_convert($thing)β Convert$thingto anTest2::Compare::*object. This will behave strictly, which means it uses these settings:implicit_end => 1β Array bounds will be checked when this object is used in a comparison. No unexpected hash keys can be present.use_code => 0β Sub references will be compared as refs (IE are these sub refs the same ref?)use_regex => 0β Regexes will be compared directly (IE are the regexes the same?)
So strict. So picky. Honestly we love a boundary queen. π
$compare = relaxed_convert($thing)β Convert$thingto anTest2::Compare::*object. This will be relaxed, which means it uses these settings:implicit_end => 0β Array bounds will not be checked when this object is used in a comparison. Unexpected hash keys can be present.use_code => 1β Sub references will be run to verify a value.use_regex => 1β Values will be checked against any regexes provided.
Chill vibes only. π
π WRITING A VARIANT OF IS/LIKE π
So you wanna build your own like()-ish thing? Periodt. Here we go:
use Test2::Compare qw/compare convert/;
sub my_like($$;$@) {
my ($got, $exp, $name, @diag) = @_;
my $ctx = context();
# A custom converter that does the same thing as the one used by like()
my $convert = sub {
my $thing = shift;
return convert(
$thing,
{
implicit_end => 0,
use_code => 1,
use_regex => 1,
}
);
};
my $delta = compare($got, $exp, $convert);
if ($delta) {
$ctx->fail($name, $delta->diag, @diag);
}
else {
$ctx->ok(1, $name);
}
$ctx->release;
return !$delta;
}
The work of a comparison tool is done by 3 entities. Three. A whole trio. The holy trinity of compare-core, if you will: π«Ά
compare()β Thecompare()function takes the structure you got, the specification you want to check against, and a"\&convert"sub that will convert anything that is not an instance of anTest2::Compare::Basesubclass into one.
This tool will use the "\&convert" function on the specification, and then produce an Test2::Compare::Delta structure that outlines all the ways the structure you got deviates from the specification. It's like a detective, but for your data. π΅οΈ
\&convertβ Converts anything that is not an instance of anTest2::Compare::Basesubclass, and turns it into one. The objects this produces are able to check that a structure matches a specification.$deltaβ An instance ofTest2::Compare::Deltais ultimately returned. This object represents all the ways in with the structure you got deviated from the specification. The delta is a tree and may contain child deltas for nested structures. Nested deltas! It's giving inheritance drama. π³
The delta is capable of rendering itself as a table β use "@lines = $delta->diag" to get the table (lines in @lines will not be terminated with "\n"). Handle your own newlines, bestie.
The convert() function provided by this package contains all the specification behavior of like() and is(). It is intended to be wrapped in a sub that passes in a configuration hash, which allows you to control the behavior.
You are free to write your own "$check = compare($thing)" function β it just needs to accept a single argument, and produce a single instance of an Test2::Compare::Base subclass. Go off. β¨
πΈ SOURCE πΈ
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/.
π§ MAINTAINERS π§
Chad Granum <exodist@cpan.org>
β AUTHORS β
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
And that's the whole page, diva. π
This was written for perl v5.42.3 and dated 2026-11-03 β screenshot it for the group chat, mwah π«Άπ
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl 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