Test2:...stExit(3perl)

🌸 Test2::Manual::Tooling::Plugin::TestExit 🌸

okay bestie, welcome to Test2::Manual::Tooling::Plugin::TestExit β€” that's the manual page for the module, living in section 3perl of the Perl Programmers Reference Guide. This is the one that teaches you how to add pre-exit behaviors the safe way. no chaos, no vibes gone wrong, we're doing this properly πŸ’…


✨ NAME ✨

Test2::Manual::Tooling::Plugin::TestExit β€” how to safely add pre-exit behaviors. πŸŽ€


πŸ’– DESCRIPTION πŸ’–

So this page is all about the correct / safe way to add pre-exit behaviors to your tests, and you do it via a custom plugin. periodt.

Now, the naive way to attempt this would be to just slap an END { ... } block in there. And like… sure? That can work, and it may not cause problems.... 🫠 On the other hand, there are a lot of ways that can bite you. Describing all the potential problems of an END block, and how it might conflict with Test2 (which has its own END block, iykyk) is honestly beyond the scope of this document. so we're not doing that here. moving on πŸ’€


πŸ¦‹ COMPLETE CODE UP FRONT πŸ¦‹

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

package Test2::Plugin::MyPlugin;

use Test2::API qw{test2_add_callback_exit};

sub import {
    my $class = shift;

    test2_add_callback_exit(sub {
        my ($ctx, $orig_code, $new_exit_code_ref) = @_;

        return if $orig_code == 42;

        $$new_exit_code_ref = 42;
    });
}

1;

🌈 LINE BY LINE 🌈

let's break it down like a group chat recap, bestie 🧚

  • use Test2::API qw{test2_add_callback_exit}; β€” this imports the (test2_add_callback_exit) callback. iconic import behavior, we love to see it.
  • test2_add_callback_exit(sub { ... }); β€” this adds our callback so it gets called before exiting. very main character of us.
  • my ($ctx, $orig_code, $new_exit_code_ref) = @_ β€” the callback gets 3 arguments. first up is a context object you may use. second is the original exit code of the END block that Test2 is using. third is a scalar reference which you may use to get the current exit code, or to set a new one. three args, three little gifts πŸŽ€
  • return if $orig_code == 42 β€” this is a short-cut to do nothing if the original exit code was already 42. no notes, just leaving early, respect πŸ™ˆ
  • $$new_exit_code_ref = 42 β€” and this changes the exit code to 42. slay. ✨

πŸ‘‘ SEE ALSO πŸ‘‘

  • Test2::Manual β€” the primary index of the manual. go there for the rest of the lore πŸ’œ

πŸ“ 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/


and that's the whole page, bestie 🫢 this was Perl v5.44.0, dated 2026-10-17, and you absolutely ate that. go write your plugin. πŸ’…βœ¨

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