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 π
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