Test2:...rabber(3perl)

πŸ’– Test2::Util::Grabber β€” the grabby bestie of Test2 πŸ’–

okay so real talk: this is a page from the 3perl manual (yes bestie, section 3, we're in Perl Programmer's Reference Guide territory ✨). Say hi to Test2::Util::Grabber πŸ‘‹


🌸 NAME 🌸

Test2::Util::Grabber β€” an Object used to temporarily intercept all events. periodt. πŸŽ€


✨ DESCRIPTION ✨

The way this works is so clean fr. Once you create this object, it will intercept and stash all events sent to the shared Test2::Hub object. Like, all of them. Every single one. She's catching them all 🧺✨

And once the object is destroyed? Events will once again be sent to the shared hub. Back to normal, no drama, no lingering side quests πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

use Test2 qw/Core 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");

# Same as flush, except it destroys the grab object.
my $events_b = $grab->finish;

After calling finish() the grab object is destroyed and $grab is set to undef. $events_a is an arrayref with the first two events. $events_b is an arrayref with the second two events. slay 🫢


πŸŽ€ EXPORTS πŸŽ€

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

# $grab is magically undef after this.
my $events = $grab->finish;

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

When you call finish() the $grab object will automagically undef itself, but only for the reference used in the method call. If you have other references to the $grab object they will not be set to undef. iykyk 😭

If the $grab object is destroyed without calling finish(), it will automatically clean up after itself and restore the parent hub. she's self-sufficient like that πŸ’…βœ¨

{
    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 one test. You can turn enforcement back one 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 πŸ‘€ (not a bug, just how it be).


πŸ”₯ METHODS πŸ”₯

  • $grab = $class->new() β€” Create a new grab object, immediately starts intercepting events. no chill, no waiting, she's on it πŸƒβ€β™€οΈπŸ’¨
  • $ar = $grab->flush() β€” Get an arrayref of all the events so far, clearing the grab objects internal list. yes queen, take them all 🧺
  • $ar = $grab->events() β€” Get an arrayref of all events so far. Does not clear the internal list. peek but don't touch πŸ’œ
  • $ar = $grab->finish() β€” Get an arrayref of all the events, then destroy the grab object. it's giving grand finale πŸŽ†
  • $hub = $grab->hub() β€” Get the hub that is used by the grab event.

🌈 ENDING BEHAVIOR 🌈

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 one test. You can turn enforcement back one 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. consider yourself warned bestie 🫢


πŸ’€ SEE ALSO πŸ’€

  • Test2::Tools::Intercept β€” Accomplish the same thing, but using blocks instead. same energy, different delivery πŸ“¦βœ¨

πŸ‘‘ SOURCE πŸ‘‘

The source code repository for Test2 can be found at https://github.com/Test-More/test-more/. go be nosy, it's encouraged πŸ•΅οΈβ€β™€οΈπŸ’–


🫢 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/


and that's the whole entire page bestie, no notes πŸ’… this was perl v5.44.0, dated 2026-10-17, living in section 3perl as Test2:...rabber(3perl). mwah, go grab some events πŸŽ€βœ¨πŸ¦‹

πŸ“¦ 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