Test2::...estExit(3)
β¨ NAME β¨
Test2::Manual::Tooling::Plugin::TestExit β brought to you by the User Contributed Perl Documentation β is literally How to safely add pre-exit behaviors. π
No cap, this is the safe-girl guide for adding pre-exit behaviors without chaos.
π¦ DESCRIPTION π¦
Okay so this page describes the correct/safe way to add pre-exit behaviors to tests via a custom plugin. Periodt.
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 bestie, here's the whole code up front β no suspense, just vibes:
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 π§
use Test2::API qw{test2_add_callback_exit};β this imports the(test2_add_callback_exit)callback. Slay.test2_add_callback_exit(sub { ... });β this adds our callback to be called before exiting. Iconic.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 the"END"block 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. Iykyk.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. Fr fr.
π SEE ALSO π
Test2::Manualβ Primary index of the manual. Go check it, bestie. π«Ά
π SOURCE π
The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/. Obsessed with a clean repo link. β¨
π MAINTAINERS π
Chad Granum <exodist@cpan.org> β the maintainer, holding it down. π
π AUTHORS π
Chad Granum <exodist@cpan.org> β same icon, author credit. π
π 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/
okay that's the whole page, bestie β you ate! π
This one is from perl v5.42.0, dated 2025-07-28, so go forth and add those pre-exit behaviors safely. Mwah! π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 Β· Artistic-1.0 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