Test2:...Custom(3perl)
π NAME π
Test2::Compare::Custom β a custom field check for comparisons, bestie. Yes, you can write your own little vibe check for any field, no cap.
π¦ DESCRIPTION π¦
Okay so picture this: you're validating a field nested deep inside a big ol' data structure, and what you need is something complicated, something unusual, something that the built-in stuff just can't give you. You could yank that field out of the structure and test it all by itself on the side β sure β or you could use this and embed the check right where it lives. That's the whole serve. This module gives you a way to write your own custom checks for fields inside deep comparisons. It's giving β¨controlβ¨.
πΈ 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 handed 4 arguments in a hash, like a little gift basket:
code => sub {
my %args = @_;
# provides got, exists, operator, name
return ref($args{got}) ? 1 : 0;
},
Also, $_ is localized to "got" β which is lowkey so nice for anyone who needs to do regex things. iykyk π«Ά
got, a.k.a.$_β the value that's being checked. That's the one. Main character.existsβ this one's a boolean. It's true if"got"exists at all. And if"exists"is false, that does not mean"got"is justundefβ it means it doesn't exist at all. Think: peeking at the value of a hash key that straight-up isn't there. πoperatorβ the operator you specified at construction. Yes, exactly that one.nameβ the name you provided at construction. Same energy.
π METHODS π
$code = $cus->codeβ hands you back the coderef that was provided at construction. The whole blueprint.$name = $cus->nameβ returns the name provided at construction. Nothing more, nothing less, periodt.$op = $cus->operatorβ returns the operator provided at construction. Iconic.$stringify = $cus->stringify_gotβ returns thestringify_gotflag that was provided at construction.$bool = $cus->verify(got => $got, exists => $bool)β the actual verification moment. This is where the magic happens. π₯
β SOURCE β
The source code repository for Test2-Suite lives right here, go be nosy:
https://github.com/Test-More/test-more/
π MAINTAINERS π
- Chad Granum
<exodist@cpan.org>
π 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 that's the whole page, you ate that π go write some custom checks bestie, the deep comparisons are waiting for you π§
perl v5.44.0 Β· 2026-10-17 Β· brought to you by the Test2:...Custom(3perl) man page β¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms