Debian Codegraph @

upstream/mageia-cauldron/man3pm/Test2::API::Context.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Test2::API::Context 3pm"
61 .TH Test2::API::Context 3pm 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Test2::API::Context \- Object to represent a testing context.
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 The context object is the primary interface for authors of testing tools
71 written with Test2. The context object represents the context in
72 which a test takes place (File and Line Number), and provides a quick way to
73 generate events from that context. The context object also takes care of
74 sending events to the correct Test2::Hub instance.
75 .SH SYNOPSIS
76 .IX Header "SYNOPSIS"
77 In general you will not be creating contexts directly. To obtain a context you
78 should always use \f(CWcontext()\fR which is exported by the Test2::API module.
79 .PP
80 .Vb 1
81 \& use Test2::API qw/context/;
82 \&
83 \& sub my_ok {
84 \& my ($bool, $name) = @_;
85 \& my $ctx = context();
86 \&
87 \& if ($bool) {
88 \& $ctx\->pass($name);
89 \& }
90 \& else {
91 \& $ctx\->fail($name);
92 \& }
93 \&
94 \& $ctx\->release; # You MUST do this!
95 \& return $bool;
96 \& }
97 .Ve
98 .PP
99 Context objects make it easy to wrap other tools that also use context. Once
100 you grab a context, any tool you call before releasing your context will
101 inherit it:
102 .PP
103 .Vb 4
104 \& sub wrapper {
105 \& my ($bool, $name) = @_;
106 \& my $ctx = context();
107 \& $ctx\->diag("wrapping my_ok");
108 \&
109 \& my $out = my_ok($bool, $name);
110 \& $ctx\->release; # You MUST do this!
111 \& return $out;
112 \& }
113 .Ve
114 .SH "CRITICAL DETAILS"
115 .IX Header "CRITICAL DETAILS"
116 .IP "you MUST always use the \fBcontext()\fR sub from Test2::API" 4
117 .IX Item "you MUST always use the context() sub from Test2::API"
118 Creating your own context via \f(CW\*(C`Test2::API::Context\->new()\*(C'\fR will almost never
119 produce a desirable result. Use \f(CWcontext()\fR which is exported by Test2::API.
120 .Sp
121 There are a handful of cases where a tool author may want to create a new
122 context by hand, which is why the \f(CW\*(C`new\*(C'\fR method exists. Unless you really know
123 what you are doing you should avoid this.
124 .IP "You MUST always release the context when done with it" 4
125 .IX Item "You MUST always release the context when done with it"
126 Releasing the context tells the system you are done with it. This gives it a
127 chance to run any necessary callbacks or cleanup tasks. If you forget to
128 release the context it will try to detect the problem and warn you about it.
129 .IP "You MUST NOT pass context objects around" 4
130 .IX Item "You MUST NOT pass context objects around"
131 When you obtain a context object it is made specifically for your tool and any
132 tools nested within. If you pass a context around you run the risk of polluting
133 other tools with incorrect context information.
134 .Sp
135 If you are certain that you want a different tool to use the same context you
136 may pass it a snapshot. \f(CW\*(C`$ctx\->snapshot\*(C'\fR will give you a shallow clone of
137 the context that is safe to pass around or store.
138 .IP "You MUST NOT store or cache a context for later" 4
139 .IX Item "You MUST NOT store or cache a context for later"
140 As long as a context exists for a given hub, all tools that try to get a
141 context will get the existing instance. If you try to store the context you
142 will pollute other tools with incorrect context information.
143 .Sp
144 If you are certain that you want to save the context for later, you can use a
145 snapshot. \f(CW\*(C`$ctx\->snapshot\*(C'\fR will give you a shallow clone of the context
146 that is safe to pass around or store.
147 .Sp
148 \&\f(CWcontext()\fR has some mechanisms to protect you if you do cause a context to
149 persist beyond the scope in which it was obtained. In practice you should not
150 rely on these protections, and they are fairly noisy with warnings.
151 .IP "You SHOULD obtain your context as soon as possible in a given tool" 4
152 .IX Item "You SHOULD obtain your context as soon as possible in a given tool"
153 You never know what tools you call from within your own tool will need a
154 context. Obtaining the context early ensures that nested tools can find the
155 context you want them to find.
156 .SH METHODS
157 .IX Header "METHODS"
158 .ie n .IP $ctx\->done_testing; 4
159 .el .IP \f(CW$ctx\fR\->done_testing; 4
160 .IX Item "$ctx->done_testing;"
161 Note that testing is finished. If no plan has been set this will generate a
162 Plan event.
163 .ie n .IP "$clone = $ctx\->\fBsnapshot()\fR" 4
164 .el .IP "\f(CW$clone\fR = \f(CW$ctx\fR\->\fBsnapshot()\fR" 4
165 .IX Item "$clone = $ctx->snapshot()"
166 This will return a shallow clone of the context. The shallow clone is safe to
167 store for later.
168 .ie n .IP $ctx\->\fBrelease()\fR 4
169 .el .IP \f(CW$ctx\fR\->\fBrelease()\fR 4
170 .IX Item "$ctx->release()"
171 This will release the context. This runs cleanup tasks, and several important
172 hooks. It will also restore \f(CW$!\fR, \f(CW$?\fR, and \f(CW$@\fR to what they were when the
173 context was created.
174 .Sp
175 \&\fBNote:\fR If a context is acquired more than once an internal refcount is kept.
176 \&\f(CWrelease()\fR decrements the ref count, none of the other actions of
177 \&\f(CWrelease()\fR will occur unless the refcount hits 0. This means only the last
178 call to \f(CWrelease()\fR will reset \f(CW$?\fR, \f(CW$!\fR, \f(CW$@\fR, and run the cleanup tasks.
179 .ie n .IP $ctx\->throw($message) 4
180 .el .IP \f(CW$ctx\fR\->throw($message) 4
181 .IX Item "$ctx->throw($message)"
182 This will throw an exception reporting to the file and line number of the
183 context. This will also release the context for you.
184 .ie n .IP $ctx\->alert($message) 4
185 .el .IP \f(CW$ctx\fR\->alert($message) 4
186 .IX Item "$ctx->alert($message)"
187 This will issue a warning from the file and line number of the context.
188 .ie n .IP "$stack = $ctx\->\fBstack()\fR" 4
189 .el .IP "\f(CW$stack\fR = \f(CW$ctx\fR\->\fBstack()\fR" 4
190 .IX Item "$stack = $ctx->stack()"
191 This will return the Test2::API::Stack instance the context used to find
192 the current hub.
193 .ie n .IP "$hub = $ctx\->\fBhub()\fR" 4
194 .el .IP "\f(CW$hub\fR = \f(CW$ctx\fR\->\fBhub()\fR" 4
195 .IX Item "$hub = $ctx->hub()"
196 This will return the Test2::Hub instance the context recognizes as the
197 current one to which all events should be sent.
198 .ie n .IP "$dbg = $ctx\->\fBtrace()\fR" 4
199 .el .IP "\f(CW$dbg\fR = \f(CW$ctx\fR\->\fBtrace()\fR" 4
200 .IX Item "$dbg = $ctx->trace()"
201 This will return the Test2::EventFacet::Trace instance used by the context.
202 .ie n .IP "$ctx\->do_in_context(\e&code, @args);" 4
203 .el .IP "\f(CW$ctx\fR\->do_in_context(\e&code, \f(CW@args\fR);" 4
204 .IX Item "$ctx->do_in_context(&code, @args);"
205 Sometimes you have a context that is not current, and you want things to use it
206 as the current one. In these cases you can call
207 \&\f(CW\*(C`$ctx\->do_in_context(sub { ... })\*(C'\fR. The codeblock will be run, and
208 anything inside of it that looks for a context will find the one on which the
209 method was called.
210 .Sp
211 This \fBDOES NOT\fR affect context on other hubs, only the hub used by the context
212 will be affected.
213 .Sp
214 .Vb 4
215 \& my $ctx = ...;
216 \& $ctx\->do_in_context(sub {
217 \& my $ctx = context(); # returns the $ctx the sub is called on
218 \& });
219 .Ve
220 .Sp
221 \&\fBNote:\fR The context will actually be cloned, the clone will be used instead of
222 the original. This allows the thread id, process id, and error variables to be correct without
223 modifying the original context.
224 .ie n .IP $ctx\->\fBrestore_error_vars()\fR 4
225 .el .IP \f(CW$ctx\fR\->\fBrestore_error_vars()\fR 4
226 .IX Item "$ctx->restore_error_vars()"
227 This will set \f(CW$!\fR, \f(CW$?\fR, and \f(CW$@\fR to what they were when the context was
228 created. There is no localization or anything done here, calling this method
229 will actually set these vars.
230 .ie n .IP "$! = $ctx\->\fBerrno()\fR" 4
231 .el .IP "$! = \f(CW$ctx\fR\->\fBerrno()\fR" 4
232 .IX Item "$! = $ctx->errno()"
233 The (numeric) value of \f(CW$!\fR when the context was created.
234 .ie n .IP "$? = $ctx\->\fBchild_error()\fR" 4
235 .el .IP "$? = \f(CW$ctx\fR\->\fBchild_error()\fR" 4
236 .IX Item "$? = $ctx->child_error()"
237 The value of \f(CW$?\fR when the context was created.
238 .ie n .IP "$@ = $ctx\->\fBeval_error()\fR" 4
239 .el .IP "$@ = \f(CW$ctx\fR\->\fBeval_error()\fR" 4
240 .IX Item "$@ = $ctx->eval_error()"
241 The value of \f(CW$@\fR when the context was created.
242 .SS "EVENT PRODUCTION METHODS"
243 .IX Subsection "EVENT PRODUCTION METHODS"
244 \&\fBWhich one do I use?\fR
245 .PP
246 The \f(CW\*(C`pass*\*(C'\fR and \f(CW\*(C`fail*\*(C'\fR are optimal if they meet your situation, using one of
247 them will always be the most optimal. That said they are optimal by eliminating
248 many features.
249 .PP
250 Method such as \f(CW\*(C`ok\*(C'\fR, and \f(CW\*(C`note\*(C'\fR are shortcuts for generating common 1\-task
251 events based on the old API, however they are forward compatible, and easy to
252 use. If these meet your needs then go ahead and use them, but please check back
253 often for alternatives that may be added.
254 .PP
255 If you want to generate new style events, events that do many things at once,
256 then you want the \f(CW\*(C`*ev2*\*(C'\fR methods. These let you directly specify which facets
257 you wish to use.
258 .ie n .IP "$event = $ctx\->\fBpass()\fR" 4
259 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->\fBpass()\fR" 4
260 .IX Item "$event = $ctx->pass()"
261 .PD 0
262 .ie n .IP "$event = $ctx\->pass($name)" 4
263 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->pass($name)" 4
264 .IX Item "$event = $ctx->pass($name)"
265 .PD
266 This will send and return an Test2::Event::Pass event. You may optionally
267 provide a \f(CW$name\fR for the assertion.
268 .Sp
269 The Test2::Event::Pass is a specially crafted and optimized event, using
270 this will help the performance of passing tests.
271 .ie n .IP "$true = $ctx\->\fBpass_and_release()\fR" 4
272 .el .IP "\f(CW$true\fR = \f(CW$ctx\fR\->\fBpass_and_release()\fR" 4
273 .IX Item "$true = $ctx->pass_and_release()"
274 .PD 0
275 .ie n .IP "$true = $ctx\->pass_and_release($name)" 4
276 .el .IP "\f(CW$true\fR = \f(CW$ctx\fR\->pass_and_release($name)" 4
277 .IX Item "$true = $ctx->pass_and_release($name)"
278 .PD
279 This is a combination of \f(CWpass()\fR and \f(CWrelease()\fR. You can use this if you do
280 not plan to do anything with the context after sending the event. This helps
281 write more clear and compact code.
282 .Sp
283 .Vb 4
284 \& sub shorthand {
285 \& my ($bool, $name) = @_;
286 \& my $ctx = context();
287 \& return $ctx\->pass_and_release($name) if $bool;
288 \&
289 \& ... Handle a failure ...
290 \& }
291 \&
292 \& sub longform {
293 \& my ($bool, $name) = @_;
294 \& my $ctx = context();
295 \&
296 \& if ($bool) {
297 \& $ctx\->pass($name);
298 \& $ctx\->release;
299 \& return 1;
300 \& }
301 \&
302 \& ... Handle a failure ...
303 \& }
304 .Ve
305 .ie n .IP "my $event = $ctx\->\fBfail()\fR" 4
306 .el .IP "my \f(CW$event\fR = \f(CW$ctx\fR\->\fBfail()\fR" 4
307 .IX Item "my $event = $ctx->fail()"
308 .PD 0
309 .ie n .IP "my $event = $ctx\->fail($name)" 4
310 .el .IP "my \f(CW$event\fR = \f(CW$ctx\fR\->fail($name)" 4
311 .IX Item "my $event = $ctx->fail($name)"
312 .ie n .IP "my $event = $ctx\->fail($name, @diagnostics)" 4
313 .el .IP "my \f(CW$event\fR = \f(CW$ctx\fR\->fail($name, \f(CW@diagnostics\fR)" 4
314 .IX Item "my $event = $ctx->fail($name, @diagnostics)"
315 .PD
316 This lets you send an Test2::Event::Fail event. You may optionally provide a
317 \&\f(CW$name\fR and \f(CW@diagnostics\fR messages.
318 .Sp
319 Diagnostics messages can be simple strings, data structures, or instances of
320 Test2::EventFacet::Info::Table (which are converted inline into the
321 Test2::EventFacet::Info structure).
322 .ie n .IP "my $false = $ctx\->\fBfail_and_release()\fR" 4
323 .el .IP "my \f(CW$false\fR = \f(CW$ctx\fR\->\fBfail_and_release()\fR" 4
324 .IX Item "my $false = $ctx->fail_and_release()"
325 .PD 0
326 .ie n .IP "my $false = $ctx\->fail_and_release($name)" 4
327 .el .IP "my \f(CW$false\fR = \f(CW$ctx\fR\->fail_and_release($name)" 4
328 .IX Item "my $false = $ctx->fail_and_release($name)"
329 .ie n .IP "my $false = $ctx\->fail_and_release($name, @diagnostics)" 4
330 .el .IP "my \f(CW$false\fR = \f(CW$ctx\fR\->fail_and_release($name, \f(CW@diagnostics\fR)" 4
331 .IX Item "my $false = $ctx->fail_and_release($name, @diagnostics)"
332 .PD
333 This is a combination of \f(CWfail()\fR and \f(CWrelease()\fR. This can be used to write
334 clearer and shorter code.
335 .Sp
336 .Vb 4
337 \& sub shorthand {
338 \& my ($bool, $name) = @_;
339 \& my $ctx = context();
340 \& return $ctx\->fail_and_release($name) unless $bool;
341 \&
342 \& ... Handle a success ...
343 \& }
344 \&
345 \& sub longform {
346 \& my ($bool, $name) = @_;
347 \& my $ctx = context();
348 \&
349 \& unless ($bool) {
350 \& $ctx\->pass($name);
351 \& $ctx\->release;
352 \& return 1;
353 \& }
354 \&
355 \& ... Handle a success ...
356 \& }
357 .Ve
358 .ie n .IP "$event = $ctx\->ok($bool, $name)" 4
359 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->ok($bool, \f(CW$name\fR)" 4
360 .IX Item "$event = $ctx->ok($bool, $name)"
361 .PD 0
362 .ie n .IP "$event = $ctx\->ok($bool, $name, \e@on_fail)" 4
363 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->ok($bool, \f(CW$name\fR, \e@on_fail)" 4
364 .IX Item "$event = $ctx->ok($bool, $name, @on_fail)"
365 .PD
366 \&\fBNOTE:\fR Use of this method is discouraged in favor of \f(CWpass()\fR and \f(CWfail()\fR
367 which produce Test2::Event::Pass and Test2::Event::Fail events. These
368 newer event types are faster and less crufty.
369 .Sp
370 This will create an Test2::Event::Ok object for you. If \f(CW$bool\fR is false
371 then an Test2::Event::Diag event will be sent as well with details about the
372 failure. If you do not want automatic diagnostics you should use the
373 \&\f(CWsend_event()\fR method directly.
374 .Sp
375 The third argument \f(CW\*(C`\e@on_fail\*(C'\fR) is an optional set of diagnostics to be sent in
376 the event of a test failure. Unlike with \f(CWfail()\fR these diagnostics must be
377 plain strings, data structures are not supported.
378 .ie n .IP "$event = $ctx\->note($message)" 4
379 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->note($message)" 4
380 .IX Item "$event = $ctx->note($message)"
381 Send an Test2::Event::Note. This event prints a message to STDOUT.
382 .ie n .IP "$event = $ctx\->diag($message)" 4
383 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->diag($message)" 4
384 .IX Item "$event = $ctx->diag($message)"
385 Send an Test2::Event::Diag. This event prints a message to STDERR.
386 .ie n .IP "$event = $ctx\->plan($max)" 4
387 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->plan($max)" 4
388 .IX Item "$event = $ctx->plan($max)"
389 .PD 0
390 .ie n .IP "$event = $ctx\->plan(0, \*(AqSKIP\*(Aq, $reason)" 4
391 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->plan(0, \*(AqSKIP\*(Aq, \f(CW$reason\fR)" 4
392 .IX Item "$event = $ctx->plan(0, 'SKIP', $reason)"
393 .PD
394 This can be used to send an Test2::Event::Plan event. This event
395 usually takes either a number of tests you expect to run. Optionally you can
396 set the expected count to 0 and give the \*(AqSKIP\*(Aq directive with a reason to
397 cause all tests to be skipped.
398 .ie n .IP "$event = $ctx\->skip($name, $reason);" 4
399 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->skip($name, \f(CW$reason\fR);" 4
400 .IX Item "$event = $ctx->skip($name, $reason);"
401 Send an Test2::Event::Skip event.
402 .ie n .IP "$event = $ctx\->bail($reason)" 4
403 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->bail($reason)" 4
404 .IX Item "$event = $ctx->bail($reason)"
405 This sends an Test2::Event::Bail event. This event will completely
406 terminate all testing.
407 .ie n .IP "$event = $ctx\->send_ev2(%facets)" 4
408 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->send_ev2(%facets)" 4
409 .IX Item "$event = $ctx->send_ev2(%facets)"
410 This lets you build and send a V2 event directly from facets. The event is
411 returned after it is sent.
412 .Sp
413 This example sends a single assertion, a note (comment for stdout in
414 Test::Builder talk) and sets the plan to 1.
415 .Sp
416 .Vb 5
417 \& my $event = $ctx\->send_event(
418 \& plan => {count => 1},
419 \& assert => {pass => 1, details => "A passing assert"},
420 \& info => [{tag => \*(AqNOTE\*(Aq, details => "This is a note"}],
421 \& );
422 .Ve
423 .ie n .IP "$event = $ctx\->build_e2(%facets)" 4
424 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->build_e2(%facets)" 4
425 .IX Item "$event = $ctx->build_e2(%facets)"
426 This is the same as \f(CWsend_ev2()\fR, except it builds and returns the event
427 without sending it.
428 .ie n .IP "$event = $ctx\->send_ev2_and_release($Type, %parameters)" 4
429 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->send_ev2_and_release($Type, \f(CW%parameters\fR)" 4
430 .IX Item "$event = $ctx->send_ev2_and_release($Type, %parameters)"
431 This is a combination of \f(CWsend_ev2()\fR and \f(CWrelease()\fR.
432 .Sp
433 .Vb 4
434 \& sub shorthand {
435 \& my $ctx = context();
436 \& return $ctx\->send_ev2_and_release(assert => {pass => 1, details => \*(Aqfoo\*(Aq});
437 \& }
438 \&
439 \& sub longform {
440 \& my $ctx = context();
441 \& my $event = $ctx\->send_ev2(assert => {pass => 1, details => \*(Aqfoo\*(Aq});
442 \& $ctx\->release;
443 \& return $event;
444 \& }
445 .Ve
446 .ie n .IP "$event = $ctx\->send_event($Type, %parameters)" 4
447 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->send_event($Type, \f(CW%parameters\fR)" 4
448 .IX Item "$event = $ctx->send_event($Type, %parameters)"
449 \&\fBIt is better to use send_ev2() in new code.\fR
450 .Sp
451 This lets you build and send an event of any type. The \f(CW$Type\fR argument should
452 be the event package name with \f(CW\*(C`Test2::Event::\*(C'\fR left off, or a fully
453 qualified package name prefixed with a \*(Aq+\*(Aq. The event is returned after it is
454 sent.
455 .Sp
456 .Vb 1
457 \& my $event = $ctx\->send_event(\*(AqOk\*(Aq, ...);
458 .Ve
459 .Sp
460 or
461 .Sp
462 .Vb 1
463 \& my $event = $ctx\->send_event(\*(Aq+Test2::Event::Ok\*(Aq, ...);
464 .Ve
465 .ie n .IP "$event = $ctx\->build_event($Type, %parameters)" 4
466 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->build_event($Type, \f(CW%parameters\fR)" 4
467 .IX Item "$event = $ctx->build_event($Type, %parameters)"
468 \&\fBIt is better to use build_ev2() in new code.\fR
469 .Sp
470 This is the same as \f(CWsend_event()\fR, except it builds and returns the event
471 without sending it.
472 .ie n .IP "$event = $ctx\->send_event_and_release($Type, %parameters)" 4
473 .el .IP "\f(CW$event\fR = \f(CW$ctx\fR\->send_event_and_release($Type, \f(CW%parameters\fR)" 4
474 .IX Item "$event = $ctx->send_event_and_release($Type, %parameters)"
475 \&\fBIt is better to use send_ev2_and_release() in new code.\fR
476 .Sp
477 This is a combination of \f(CWsend_event()\fR and \f(CWrelease()\fR.
478 .Sp
479 .Vb 4
480 \& sub shorthand {
481 \& my $ctx = context();
482 \& return $ctx\->send_event_and_release(Pass => { name => \*(Aqfoo\*(Aq });
483 \& }
484 \&
485 \& sub longform {
486 \& my $ctx = context();
487 \& my $event = $ctx\->send_event(Pass => { name => \*(Aqfoo\*(Aq });
488 \& $ctx\->release;
489 \& return $event;
490 \& }
491 .Ve
492 .SH HOOKS
493 .IX Header "HOOKS"
494 There are 2 types of hooks, init hooks, and release hooks. As the names
495 suggest, these hooks are triggered when contexts are created or released.
496 .SS "INIT HOOKS"
497 .IX Subsection "INIT HOOKS"
498 These are called whenever a context is initialized. That means when a new
499 instance is created. These hooks are \fBNOT\fR called every time something
500 requests a context, just when a new one is created.
501 .PP
502 \fIGLOBAL\fR
503 .IX Subsection "GLOBAL"
504 .PP
505 This is how you add a global init callback. Global callbacks happen for every
506 context for any hub or stack.
507 .PP
508 .Vb 4
509 \& Test2::API::test2_add_callback_context_init(sub {
510 \& my $ctx = shift;
511 \& ...
512 \& });
513 .Ve
514 .PP
515 \fIPER HUB\fR
516 .IX Subsection "PER HUB"
517 .PP
518 This is how you add an init callback for all contexts created for a given hub.
519 These callbacks will not run for other hubs.
520 .PP
521 .Vb 4
522 \& $hub\->add_context_init(sub {
523 \& my $ctx = shift;
524 \& ...
525 \& });
526 .Ve
527 .PP
528 \fIPER CONTEXT\fR
529 .IX Subsection "PER CONTEXT"
530 .PP
531 This is how you specify an init hook that will only run if your call to
532 \&\f(CWcontext()\fR generates a new context. The callback will be ignored if
533 \&\f(CWcontext()\fR is returning an existing context.
534 .PP
535 .Vb 4
536 \& my $ctx = context(on_init => sub {
537 \& my $ctx = shift;
538 \& ...
539 \& });
540 .Ve
541 .SS "RELEASE HOOKS"
542 .IX Subsection "RELEASE HOOKS"
543 These are called whenever a context is released. That means when the last
544 reference to the instance is about to be destroyed. These hooks are \fBNOT\fR
545 called every time \f(CW\*(C`$ctx\->release\*(C'\fR is called.
546 .PP
547 \fIGLOBAL\fR
548 .IX Subsection "GLOBAL"
549 .PP
550 This is how you add a global release callback. Global callbacks happen for every
551 context for any hub or stack.
552 .PP
553 .Vb 4
554 \& Test2::API::test2_add_callback_context_release(sub {
555 \& my $ctx = shift;
556 \& ...
557 \& });
558 .Ve
559 .PP
560 \fIPER HUB\fR
561 .IX Subsection "PER HUB"
562 .PP
563 This is how you add a release callback for all contexts created for a given
564 hub. These callbacks will not run for other hubs.
565 .PP
566 .Vb 4
567 \& $hub\->add_context_release(sub {
568 \& my $ctx = shift;
569 \& ...
570 \& });
571 .Ve
572 .PP
573 \fIPER CONTEXT\fR
574 .IX Subsection "PER CONTEXT"
575 .PP
576 This is how you add release callbacks directly to a context. The callback will
577 \&\fBALWAYS\fR be added to the context that gets returned, it does not matter if a
578 new one is generated, or if an existing one is returned.
579 .PP
580 .Vb 4
581 \& my $ctx = context(on_release => sub {
582 \& my $ctx = shift;
583 \& ...
584 \& });
585 .Ve
586 .SH "THIRD PARTY META\-DATA"
587 .IX Header "THIRD PARTY META-DATA"
588 This object consumes Test2::Util::ExternalMeta which provides a consistent
589 way for you to attach meta\-data to instances of this class. This is useful for
590 tools, plugins, and other extensions.
591 .SH SOURCE
592 .IX Header "SOURCE"
593 The source code repository for Test2 can be found at
594 <https://github.com/Test\-More/test\-more/>.
595 .SH MAINTAINERS
596 .IX Header "MAINTAINERS"
597 .IP "Chad Granum <[email protected]>" 4
598 .IX Item "Chad Granum <[email protected]>"
599 .SH AUTHORS
600 .IX Header "AUTHORS"
601 .IP "Chad Granum <[email protected]>" 4
602 .IX Item "Chad Granum <[email protected]>"
603 .PD 0
604 .IP "Kent Fredric <[email protected]>" 4
605 .IX Item "Kent Fredric <[email protected]>"
606 .PD
607 .SH COPYRIGHT
608 .IX Header "COPYRIGHT"
609 Copyright Chad Granum <[email protected]>.
610 .PP
611 This program is free software; you can redistribute it and/or
612 modify it under the same terms as Perl itself.
613 .PP
614 See <https://dev.perl.org/licenses/>