Test2::Tools::Grab(3)

πŸ“¦ version 1.302214, in opensuse-leap 16.0

✨ NAME ✨

Okay bestie, this is the Test2::Tools::Grab(3) page from User Contributed Perl Documentation.

Test2::Tools::Grab β€” temporarily intercept all events without adding a scope level. Periodt. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

This package provides a function that returns an object that grabs all events. Once the object is destroyed, events will once again be sent to the main hub. It’s giving β€œevent trap, but make it temporary.” ✨


🌸 SYNOPSIS 🌸

use Test2::Tools::Grab;

my $grab = grab();

# Generate some events, they are intercepted.
ok(1, "pass");
ok(0, "fail");

my $events_a = $grab->flush;

# Generate some more events, they are intercepted.
ok(1, "pass");
ok(0, "fail");

my $events_b = $grab->finish;

πŸŽ€ EXPORTS πŸŽ€

  • $grab = grab() β€” this lets you intercept all events for a section of code without adding anything to your call stack. This is useful for things that are sensitive to changes in the stack depth. Iykyk. 🫢
my $grab = grab();
    ok(1, 'foo');
    ok(0, 'bar');

my $events = $grab->finish;

is(@$events, 2, "grabbed 2 events.");

If the $grab object is destroyed without calling finish(), it will automatically clean up after itself and restore the parent hub. No cap, it really does tidy up after itself. 🧚

{
    my $grab = grab();
    # Things are grabbed
}
# Things are back to normal

By default the hub used has no_ending set to true. This will prevent the hub from enforcing that you issued a plan and ran at least 1 test. You can turn enforcement back on like this:

$grab->hub->set_no_ending(0);

With no_ending turned off, finish will run the post-test checks to enforce the plan and that tests were run. In many cases this will result in additional events in your events array. So like… expect extra guests at the party. πŸŽ‰


πŸ‘€ SEE ALSO πŸ‘€

  • Test2::Util::Grabber β€” the object constructed and returned by this tool.

πŸ”₯ SOURCE πŸ”₯

The source code repository for Test2 can be found at https://github.com/Test-More/test-more/.


πŸ‘‘ MAINTAINERS πŸ‘‘

Chad Granum Chad Granum <exodist@cpan.org>


πŸ’– AUTHORS πŸ’–

Chad Granum Chad Granum <exodist@cpan.org>


Copyright Chad Granum 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 vibe: this page is from perl v5.42.0, dated 2025-07-28, and it’s the Test2::Tools::Grab(3) manual page. Bye bestie, go grab those events responsibly πŸ’œπŸŽ€

πŸ“¦ where this page comes from