Test2:...Starts(3perl)
π Test2::Manual::Tooling::Plugin::ToolStarts π
okay bestie, welcome to Test2::Manual::Tooling::Plugin::ToolStarts(3perl) β the little corner of the Perl docs where we learn how to make plugins that do stuff the exact moment a tool starts doing stuff. pull up a chair, we're writing callbacks π
β¨ NAME β¨
Test2::Manual::Tooling::Plugin::ToolStarts β how to add behaviors that occur when a tool starts work. lowkey iconic, highkey useful.
πΈ DESCRIPTION πΈ
This tutorial will help you write plugins that have behavior when a tool starts. Every single tool should start by acquiring a context object β that's just the vibe. And this tutorial? It shows you the hooks you can use to take advantage of that context acquisition. no cap. π
π¦ COMPLETE CODE UP FRONT π¦
here's the whole thing, no waiting, no suspense, fr fr:
package Test2::Plugin::MyPlugin;
use Test2::API qw{
test2_add_callback_context_init
test2_add_callback_context_acquire
};
sub import {
my $class = shift;
# Let us know every time a tool requests a context, and give us a
# chance to modify the parameters before we find it.
test2_add_callback_context_acquire(sub {
my $params_ref = shift;
print "A tool has requested the context\n";
});
# Callback every time a new context is created, not called if an
# existing context is found.
test2_add_callback_context_init(sub {
my $ctx_ref = shift;
print "A new context was created\n";
});
}
1;
π LINE BY LINE π
β use Test2::API qw{test2_add_callback_context_init test2_add_callback_context_acquire};
This imports the test2_add_callback_context_init() and test2_add_callback_context_acquire() callbacks. literally the two besties we need for this whole quest.
β test2_add_callback_context_acquire(sub { ... })
This is where we add our callback for context acquisition. Every time Test2::API::context() is called, the callback will be run. every. single. time. obsessed. π
β my $params_ref = shift
In the test2_add_callback_context_acquire() callbacks we get exactly 1 argument, a reference to the parameters that context() will use to find the context. one (1) argument, that's it, that's the whole gift π
β print "A tool has requested the context\n"
Print a notification whenever a tool asks for a context. just a lil shoutout in the chat, yk? π£
β test2_add_callback_context_init(sub { ... })
Add our context init callback. These callbacks are triggered whenever a completely new context is created. This is not called if an existing context is found. In short, this only fires off for the top level tool, not nested tools. so it's giving "main character only, no entourage" β¨
β my $ctx_ref = shift
The coderefs for test2_add_callback_context_init() will receive exactly 1 argument, the newly created context. one arg, freshly born, so cute πΌ
β print "A new context was created\n"
Print a notification whenever a new context is created.
π SEE ALSO π
Test2::Manualβ Primary index of the manual. your one-stop directory, iykyk πΊοΈ
π¦ SOURCE π¦
The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/. go forth and star it, bestie β
π«Ά 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 page, babe π this was perl v5.44.0, dated 2026-10-17, and you were reading Test2:...Starts(3perl) the entire time. mwah π periodt.
π¦ 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