TEST2::MANUAL::TOOLING::FIRSTTOOL(3perl)

πŸ’– Test2::Manual::Tooling::FirstTool πŸ’–

okay bestie, so this is the Perl Programmers Reference Guide page for Test2::Manual::Tooling::FirstTool β€” and fr fr, this is the moment you write your FIRST EVER Test2 tool. we're doing it. no cap. πŸŽ€


✨ NAME ✨

Test2::Manual::Tooling::FirstTool β€” Write your first tool with Test2. πŸ’…


🌸 DESCRIPTION 🌸

This tutorial will help you write your very first tool by cloning the ok() tool. like, literally just copying ok()'s whole vibe and making it your own. it's giving "first day at a new job but your big sister already works there" πŸ¦‹


πŸ‘‘ COMPLETE CODE UP FRONT πŸ‘‘

here's the whole thing, no suspense, we're not gatekeeping:

    package Test2::Tools::MyOk;
    use strict;
    use warnings;

    use Test2::API qw/context/;

    use base 'Exporter';
    our @EXPORT = qw/ok/;

    sub ok($;$@) {
        my ($bool, $name, @diag) = @_;

        my $ctx = context();

        return $ctx->pass_and_release($name) if $bool;
        return $ctx->fail_and_release($name, @diag);
    }

    1;

ate. periodt. πŸ”₯


🧚 LINE BY LINE 🧚

sub ok($;$@) {

In this case we are emulating the ok() function exported by Test2::Tools::Basic. we're basically doing an impression of it, and honestly? we're nailing it πŸ’…

ok() and similar test tools use prototypes to enforce argument parsing. Your test tools do not necessarily need prototypes, like any perl function you need to make the decision based on how it is used.

The prototype requires at least 1 argument, which will be forced into a scalar context. The second argument is optional, and is also forced to be scalar β€” it is the name of the test. Any remaining arguments are treated as diagnostics messages that will only be used if the test failed. so like: one mandatory thing, one optional name, and then a lil pile of extra notes that only show up when things go wrong 😭

my ($bool, $name, @diag) = @_;

This line does not need much explanation, we are simply grabbing the args. lowkey the most chill line in the whole file πŸ“

my $ctx = context();

This is a vital line in ALL tools. The context object is the primary API for test tools. You MUST get a context if you want to issue any events, such as making assertions. Further, the context is responsible for making sure failures are attributed to the correct file and line number. like, the whole point β€” if you skip this, your failures have no idea where they came from 😭

Note: A test function MUST always release the context when it is done, you cannot simply let it fall out of scope and be garbage collected. Test2 does a pretty good job of yelling at you if you make this mistake. πŸ—£οΈ
Note: You MUST NOT ever store or pass around a real context object. If you wish to hold on to a context for any reason you must use clone to make a copy my $copy = $ctx->clone. The copy may be passed around or stored, but the original MUST be released when you are done with it. do not be out here passing your original around like it's a group project, bestie πŸ™ˆ

return $ctx->pass_and_release($name) if $bool;

When $bool is true, this line uses the context object to issue a Test2::Event::Pass event. Along with issuing the event this will also release the context object and return true.

This is short form for:

    if($bool) {
        $ctx->pass($name);
        $ctx->release;
        return 1;
    }

return $ctx->fail_and_release($name, @diag);

This line issues a Test2::Event::Fail event, releases the context object, and returns false. The fail event will include any diagnostics messages from the @diag array. it said what it said πŸ’€

This is short form for:

    $ctx->fail($name, @diag);
    $ctx->release;
    return 0;

🌈 CONTEXT OBJECT DOCUMENTATION 🌈

Test2::API::Context is the place to read up on what methods the context provides. go read it, it's iconic, it's the main character of this whole page πŸ‘‘


⭐ SEE ALSO ⭐

  • Test2::Manual β€” Primary index of the manual.

🫢 SOURCE 🫢

The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/.


πŸ’œ MAINTAINERS πŸ’œ

Chad Granum <exodist@cpan.org>


🌸 AUTHORS 🌸

Chad Granum <exodist@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 bestie that's the whole page, you're officially a tool author, go write something slay πŸ¦‹βœ¨

this was perl v5.44.0, dated 2026-10-17, and it's Test2::Manual::Tooling::FirstTool(3perl) β€” mwah, bye! πŸ’…πŸ’–

πŸ“¦ 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