Test2::...:Custom(3pm)
β¨ NAME β¨
Okay so this page is Test2::...:Custom(3pm) from the Perl Programmers Reference Guide, and the actual thing it's documenting is Test2::Compare::Custom β the custom field check for comparisons, bestie π
π¦ DESCRIPTION π¦
So picture this: you're validating a field nested way down inside a deep data structure, and you wanna do something complicated or unusual with it. You could yank it out of the structure and test it separately... or you can use this and embed the check right there. It's literally giving you a way to write custom checks for fields in deep comparisons. Obsessed π«Ά
πΈ SYNOPSIS πΈ
my $cus = Test2::Compare::Custom->new(
name => 'IsRef',
operator => 'ref(...)',
stringify_got => 1,
code => sub {
my %args = @_;
return $args{got} ? 1 : 0;
},
);
# Pass
is(
{ a => 1, ref => {}, b => 2 },
{ a => 1, ref => $cus, b => 2 },
"This will pass"
);
# Fail
is(
{a => 1, ref => 'notref', b => 2},
{a => 1, ref => $cus, b => 2},
"This will fail"
);
π ARGUMENTS π
Your custom sub gets passed 4 arguments in a hash. No cap:
code => sub {
my %args = @_;
# provides got, exists, operator, name
return ref($args{got}) ? 1 : 0;
},
Also, $_ is localized to got, which is so convenient if you're a regex girlie who needs to use regexes β¨
got,$_β the value to be checked. Same value, two ways to get at it πexistsβ this is a boolean. It's true ifgotexists at all. Ifexistsis false, it meansgotisn't just simplyundefβ it doesn't exist at all (think: checking the value of a hash key that does not exist). Big difference, lowkey πoperatorβ the operator you specified at construction.nameβ the name you provided at construction.
π METHODS π
$code = $cus->codeβ hands you back the coderef you gave it at construction π$name = $cus->nameβ returns the name you provided at construction.$op = $cus->operatorβ returns the operator you provided at construction.$stringify = $cus->stringify_gotβ returns thestringify_gotflag you provided at construction.$bool = $cus->verify(got => $got, exists => $bool)β the verify call, right there in its full signature form.
β SOURCE β
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go give it a star, it's iconic π
π MAINTAINERS π
Chad Granum <exodist@cpan.org> β carrying the whole thing fr fr π
π§ AUTHORS π§
- Chad Granum
<exodist@cpan.org> - Daniel BΓΆhmer
<dboehmer@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/
Okay so real talk, <exodist@cpan.org> is the copyright holder and this whole thing runs on the same terms as Perl itself, so keep that in mind before you redistribute it π«Ά
And that's the whole vibe check β page brought to you by perl v5.42.3, dated 2026-11-03, straight out of the Perl Programmers Reference Guide πβ¨ππ¦
π¦ 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