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 just undef β€” 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 the stringify_got flag 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 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