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 theENDblock 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 already42. no notes, just leaving early, respect π$$new_exit_code_ref = 42β and this changes the exit code to42. 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 π
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