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 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.
  • 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 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