Test2::...estExit(3pm)
πΈ NAME πΈ
okay bestie, welcome to Test2::Manual::Tooling::Plugin::TestExit from the Perl Programmers Reference Guide π β this page is literally about how to safely add pre-exit behaviors β¨
π DESCRIPTION π
This describes the correct/safe way to add pre-exit behaviors to tests via a custom plugin π«Ά
The naive way to attempt this would be to add an END { ... } block. That can work, and 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) is beyond the scope of this document π
β¨ COMPLETE CODE UP FRONT β¨
okay, full code drop, no cap:
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 walk it line by line, bestie π§
use Test2::API qw{test2_add_callback_exit};β this imports the(test2_add_callback_exit)callback.test2_add_callback_exit(sub { ... });β this adds our callback to be called before exiting.my ($ctx, $orig_code, $new_exit_code_ref) = @_β the callback gets 3 arguments. First is a context object you may use. The second is the original exit code of theENDblock Test2 is using. The third argument is a scalar reference which you may use to get the current exit code, or set a new one.return if $orig_code == 42β this is a short-cut to do nothing if the original exit code was already 42.$$new_exit_code_ref = 42β this changes the exit code to 42.
π 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 π
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/
mwah π that's the whole page, bestie β it's from perl v5.42.3, dated 2026-11-03, and the footer says Test2::...estExit(3pm) β periodt β¨π«Ά
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms