Debian Codegraph @

upstream/debian-unstable/man3/NEXT.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (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 .\"
57 .IX Title "NEXT 3perl"
58 .TH NEXT 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 NEXT \- Provide a pseudo\-class NEXT (et al) that allows method redispatch
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use NEXT;
69 \&
70 \& package P;
71 \& sub P::method { print "$_[0]: P method\en"; $_[0]\->NEXT::method() }
72 \& sub P::DESTROY { print "$_[0]: P dtor\en"; $_[0]\->NEXT::DESTROY() }
73 \&
74 \& package Q;
75 \& use base qw( P );
76 \& sub Q::AUTOLOAD { print "$_[0]: Q AUTOLOAD\en"; $_[0]\->NEXT::AUTOLOAD() }
77 \& sub Q::DESTROY { print "$_[0]: Q dtor\en"; $_[0]\->NEXT::DESTROY() }
78 \&
79 \& package R;
80 \& sub R::method { print "$_[0]: R method\en"; $_[0]\->NEXT::method() }
81 \& sub R::AUTOLOAD { print "$_[0]: R AUTOLOAD\en"; $_[0]\->NEXT::AUTOLOAD() }
82 \& sub R::DESTROY { print "$_[0]: R dtor\en"; $_[0]\->NEXT::DESTROY() }
83 \&
84 \& package S;
85 \& use base qw( Q R );
86 \& sub S::method { print "$_[0]: S method\en"; $_[0]\->NEXT::method() }
87 \& sub S::AUTOLOAD { print "$_[0]: S AUTOLOAD\en"; $_[0]\->NEXT::AUTOLOAD() }
88 \& sub S::DESTROY { print "$_[0]: S dtor\en"; $_[0]\->NEXT::DESTROY() }
89 \&
90 \& package main;
91 \&
92 \& my $obj = bless {}, "S";
93 \&
94 \& $obj\->method(); # Calls S::method, P::method, R::method
95 \& $obj\->missing_method(); # Calls S::AUTOLOAD, Q::AUTOLOAD, R::AUTOLOAD
96 \&
97 \& # Clean\-up calls S::DESTROY, Q::DESTROY, P::DESTROY, R::DESTROY
98 .Ve
99 .SH DESCRIPTION
100 .IX Header "DESCRIPTION"
101 The \f(CW\*(C`NEXT\*(C'\fR module adds a pseudoclass named \f(CW\*(C`NEXT\*(C'\fR to any program
102 that uses it. If a method \f(CW\*(C`m\*(C'\fR calls \f(CW\*(C`$self\->NEXT::m()\*(C'\fR, the call to
103 \&\f(CW\*(C`m\*(C'\fR is redispatched as if the calling method had not originally been found.
104 .PP
105 \&\fBNote:\fR before using this module,
106 you should look at next::method <https://metacpan.org/pod/mro#next::method>
107 in the core mro module.
108 \&\f(CW\*(C`mro\*(C'\fR has been a core module since Perl 5.9.5.
109 .PP
110 In other words, a call to \f(CW\*(C`$self\->NEXT::m()\*(C'\fR resumes the depth-first,
111 left-to-right search of \f(CW$self\fR's class hierarchy that resulted in the
112 original call to \f(CW\*(C`m\*(C'\fR.
113 .PP
114 Note that this is not the same thing as \f(CW\*(C`$self\->SUPER::m()\*(C'\fR, which
115 begins a new dispatch that is restricted to searching the ancestors
116 of the current class. \f(CW\*(C`$self\->NEXT::m()\*(C'\fR can backtrack
117 past the current class \-\- to look for a suitable method in other
118 ancestors of \f(CW$self\fR \-\- whereas \f(CW\*(C`$self\->SUPER::m()\*(C'\fR cannot.
119 .PP
120 A typical use would be in the destructors of a class hierarchy,
121 as illustrated in the SYNOPSIS above. Each class in the hierarchy
122 has a DESTROY method that performs some class-specific action
123 and then redispatches the call up the hierarchy. As a result,
124 when an object of class S is destroyed, the destructors of \fIall\fR
125 its parent classes are called (in depth-first, left-to-right order).
126 .PP
127 Another typical use of redispatch would be in \f(CW\*(C`AUTOLOAD\*(C'\fR'ed methods.
128 If such a method determined that it was not able to handle a
129 particular call, it might choose to redispatch that call, in the
130 hope that some other \f(CW\*(C`AUTOLOAD\*(C'\fR (above it, or to its left) might
131 do better.
132 .PP
133 By default, if a redispatch attempt fails to find another method
134 elsewhere in the objects class hierarchy, it quietly gives up and does
135 nothing (but see "Enforcing redispatch"). This gracious acquiescence
136 is also unlike the (generally annoying) behaviour of \f(CW\*(C`SUPER\*(C'\fR, which
137 throws an exception if it cannot redispatch.
138 .PP
139 Note that it is a fatal error for any method (including \f(CW\*(C`AUTOLOAD\*(C'\fR)
140 to attempt to redispatch any method that does not have the
141 same name. For example:
142 .PP
143 .Vb 1
144 \& sub S::oops { print "oops!\en"; $_[0]\->NEXT::other_method() }
145 .Ve
146 .SS "Enforcing redispatch"
147 .IX Subsection "Enforcing redispatch"
148 It is possible to make \f(CW\*(C`NEXT\*(C'\fR redispatch more demandingly (i.e. like
149 \&\f(CW\*(C`SUPER\*(C'\fR does), so that the redispatch throws an exception if it cannot
150 find a "next" method to call.
151 .PP
152 To do this, simple invoke the redispatch as:
153 .PP
154 .Vb 1
155 \& $self\->NEXT::ACTUAL::method();
156 .Ve
157 .PP
158 rather than:
159 .PP
160 .Vb 1
161 \& $self\->NEXT::method();
162 .Ve
163 .PP
164 The \f(CW\*(C`ACTUAL\*(C'\fR tells \f(CW\*(C`NEXT\*(C'\fR that there must actually be a next method to call,
165 or it should throw an exception.
166 .PP
167 \&\f(CW\*(C`NEXT::ACTUAL\*(C'\fR is most commonly used in \f(CW\*(C`AUTOLOAD\*(C'\fR methods, as a means to
168 decline an \f(CW\*(C`AUTOLOAD\*(C'\fR request, but preserve the normal exception-on-failure
169 semantics:
170 .PP
171 .Vb 8
172 \& sub AUTOLOAD {
173 \& if ($AUTOLOAD =~ /foo|bar/) {
174 \& # handle here
175 \& }
176 \& else { # try elsewhere
177 \& shift()\->NEXT::ACTUAL::AUTOLOAD(@_);
178 \& }
179 \& }
180 .Ve
181 .PP
182 By using \f(CW\*(C`NEXT::ACTUAL\*(C'\fR, if there is no other \f(CW\*(C`AUTOLOAD\*(C'\fR to handle the
183 method call, an exception will be thrown (as usually happens in the absence of
184 a suitable \f(CW\*(C`AUTOLOAD\*(C'\fR).
185 .SS "Avoiding repetitions"
186 .IX Subsection "Avoiding repetitions"
187 If \f(CW\*(C`NEXT\*(C'\fR redispatching is used in the methods of a "diamond" class hierarchy:
188 .PP
189 .Vb 5
190 \& # A B
191 \& # / \e /
192 \& # C D
193 \& # \e /
194 \& # E
195 \&
196 \& use NEXT;
197 \&
198 \& package A;
199 \& sub foo { print "called A::foo\en"; shift\->NEXT::foo() }
200 \&
201 \& package B;
202 \& sub foo { print "called B::foo\en"; shift\->NEXT::foo() }
203 \&
204 \& package C; @ISA = qw( A );
205 \& sub foo { print "called C::foo\en"; shift\->NEXT::foo() }
206 \&
207 \& package D; @ISA = qw(A B);
208 \& sub foo { print "called D::foo\en"; shift\->NEXT::foo() }
209 \&
210 \& package E; @ISA = qw(C D);
211 \& sub foo { print "called E::foo\en"; shift\->NEXT::foo() }
212 \&
213 \& E\->foo();
214 .Ve
215 .PP
216 then derived classes may (re\-)inherit base-class methods through two or
217 more distinct paths (e.g. in the way \f(CW\*(C`E\*(C'\fR inherits \f(CW\*(C`A::foo\*(C'\fR twice \-\-
218 through \f(CW\*(C`C\*(C'\fR and \f(CW\*(C`D\*(C'\fR). In such cases, a sequence of \f(CW\*(C`NEXT\*(C'\fR redispatches
219 will invoke the multiply inherited method as many times as it is
220 inherited. For example, the above code prints:
221 .PP
222 .Vb 6
223 \& called E::foo
224 \& called C::foo
225 \& called A::foo
226 \& called D::foo
227 \& called A::foo
228 \& called B::foo
229 .Ve
230 .PP
231 (i.e. \f(CW\*(C`A::foo\*(C'\fR is called twice).
232 .PP
233 In some cases this \fImay\fR be the desired effect within a diamond hierarchy,
234 but in others (e.g. for destructors) it may be more appropriate to
235 call each method only once during a sequence of redispatches.
236 .PP
237 To cover such cases, you can redispatch methods via:
238 .PP
239 .Vb 1
240 \& $self\->NEXT::DISTINCT::method();
241 .Ve
242 .PP
243 rather than:
244 .PP
245 .Vb 1
246 \& $self\->NEXT::method();
247 .Ve
248 .PP
249 This causes the redispatcher to only visit each distinct \f(CW\*(C`method\*(C'\fR method
250 once. That is, to skip any classes in the hierarchy that it has
251 already visited during redispatch. So, for example, if the
252 previous example were rewritten:
253 .PP
254 .Vb 2
255 \& package A;
256 \& sub foo { print "called A::foo\en"; shift\->NEXT::DISTINCT::foo() }
257 \&
258 \& package B;
259 \& sub foo { print "called B::foo\en"; shift\->NEXT::DISTINCT::foo() }
260 \&
261 \& package C; @ISA = qw( A );
262 \& sub foo { print "called C::foo\en"; shift\->NEXT::DISTINCT::foo() }
263 \&
264 \& package D; @ISA = qw(A B);
265 \& sub foo { print "called D::foo\en"; shift\->NEXT::DISTINCT::foo() }
266 \&
267 \& package E; @ISA = qw(C D);
268 \& sub foo { print "called E::foo\en"; shift\->NEXT::DISTINCT::foo() }
269 \&
270 \& E\->foo();
271 .Ve
272 .PP
273 then it would print:
274 .PP
275 .Vb 5
276 \& called E::foo
277 \& called C::foo
278 \& called A::foo
279 \& called D::foo
280 \& called B::foo
281 .Ve
282 .PP
283 and omit the second call to \f(CW\*(C`A::foo\*(C'\fR (since it would not be distinct
284 from the first call to \f(CW\*(C`A::foo\*(C'\fR).
285 .PP
286 Note that you can also use:
287 .PP
288 .Vb 1
289 \& $self\->NEXT::DISTINCT::ACTUAL::method();
290 .Ve
291 .PP
292 or:
293 .PP
294 .Vb 1
295 \& $self\->NEXT::ACTUAL::DISTINCT::method();
296 .Ve
297 .PP
298 to get both unique invocation \fIand\fR exception-on-failure.
299 .PP
300 Note that, for historical compatibility, you can also use
301 \&\f(CW\*(C`NEXT::UNSEEN\*(C'\fR instead of \f(CW\*(C`NEXT::DISTINCT\*(C'\fR.
302 .SS "Invoking all versions of a method with a single call"
303 .IX Subsection "Invoking all versions of a method with a single call"
304 Yet another pseudo-class that \f(CW\*(C`NEXT\*(C'\fR provides is \f(CW\*(C`EVERY\*(C'\fR.
305 Its behaviour is considerably simpler than that of the \f(CW\*(C`NEXT\*(C'\fR family.
306 A call to:
307 .PP
308 .Vb 1
309 \& $obj\->EVERY::foo();
310 .Ve
311 .PP
312 calls \fIevery\fR method named \f(CW\*(C`foo\*(C'\fR that the object in \f(CW$obj\fR has inherited.
313 That is:
314 .PP
315 .Vb 1
316 \& use NEXT;
317 \&
318 \& package A; @ISA = qw(B D X);
319 \& sub foo { print "A::foo " }
320 \&
321 \& package B; @ISA = qw(D X);
322 \& sub foo { print "B::foo " }
323 \&
324 \& package X; @ISA = qw(D);
325 \& sub foo { print "X::foo " }
326 \&
327 \& package D;
328 \& sub foo { print "D::foo " }
329 \&
330 \& package main;
331 \&
332 \& my $obj = bless {}, \*(AqA\*(Aq;
333 \& $obj\->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
334 .Ve
335 .PP
336 Prefixing a method call with \f(CW\*(C`EVERY::\*(C'\fR causes every method in the
337 object's hierarchy with that name to be invoked. As the above example
338 illustrates, they are not called in Perl's usual "left-most-depth-first"
339 order. Instead, they are called "breadth-first-dependency-wise".
340 .PP
341 That means that the inheritance tree of the object is traversed breadth-first
342 and the resulting order of classes is used as the sequence in which methods
343 are called. However, that sequence is modified by imposing a rule that the
344 appropriate method of a derived class must be called before the same method of
345 any ancestral class. That's why, in the above example, \f(CW\*(C`X::foo\*(C'\fR is called
346 before \f(CW\*(C`D::foo\*(C'\fR, even though \f(CW\*(C`D\*(C'\fR comes before \f(CW\*(C`X\*(C'\fR in \f(CW@B::ISA\fR.
347 .PP
348 In general, there's no need to worry about the order of calls. They will be
349 left-to-right, breadth-first, most-derived-first. This works perfectly for
350 most inherited methods (including destructors), but is inappropriate for
351 some kinds of methods (such as constructors, cloners, debuggers, and
352 initializers) where it's more appropriate that the least-derived methods be
353 called first (as more-derived methods may rely on the behaviour of their
354 "ancestors"). In that case, instead of using the \f(CW\*(C`EVERY\*(C'\fR pseudo-class:
355 .PP
356 .Vb 1
357 \& $obj\->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
358 .Ve
359 .PP
360 you can use the \f(CW\*(C`EVERY::LAST\*(C'\fR pseudo-class:
361 .PP
362 .Vb 1
363 \& $obj\->EVERY::LAST::foo(); # prints" D::foo X::foo B::foo A::foo
364 .Ve
365 .PP
366 which reverses the order of method call.
367 .PP
368 Whichever version is used, the actual methods are called in the same
369 context (list, scalar, or void) as the original call via \f(CW\*(C`EVERY\*(C'\fR, and return:
370 .IP \(bu 4
371 A hash of array references in list context. Each entry of the hash has the
372 fully qualified method name as its key and a reference to an array containing
373 the method's list-context return values as its value.
374 .IP \(bu 4
375 A reference to a hash of scalar values in scalar context. Each entry of the hash has the
376 fully qualified method name as its key and the method's scalar-context return values as its value.
377 .IP \(bu 4
378 Nothing in void context (obviously).
379 .ie n .SS "Using ""EVERY"" methods"
380 .el .SS "Using \f(CWEVERY\fP methods"
381 .IX Subsection "Using EVERY methods"
382 The typical way to use an \f(CW\*(C`EVERY\*(C'\fR call is to wrap it in another base
383 method, that all classes inherit. For example, to ensure that every
384 destructor an object inherits is actually called (as opposed to just the
385 left-most-depth-first-est one):
386 .PP
387 .Vb 2
388 \& package Base;
389 \& sub DESTROY { $_[0]\->EVERY::Destroy }
390 \&
391 \& package Derived1;
392 \& use base \*(AqBase\*(Aq;
393 \& sub Destroy {...}
394 \&
395 \& package Derived2;
396 \& use base \*(AqBase\*(Aq, \*(AqDerived1\*(Aq;
397 \& sub Destroy {...}
398 .Ve
399 .PP
400 et cetera. Every derived class than needs its own clean-up
401 behaviour simply adds its own \f(CW\*(C`Destroy\*(C'\fR method (\fInot\fR a \f(CW\*(C`DESTROY\*(C'\fR method),
402 which the call to \f(CW\*(C`EVERY::LAST::Destroy\*(C'\fR in the inherited destructor
403 then correctly picks up.
404 .PP
405 Likewise, to create a class hierarchy in which every initializer inherited by
406 a new object is invoked:
407 .PP
408 .Vb 6
409 \& package Base;
410 \& sub new {
411 \& my ($class, %args) = @_;
412 \& my $obj = bless {}, $class;
413 \& $obj\->EVERY::LAST::Init(\e%args);
414 \& }
415 \&
416 \& package Derived1;
417 \& use base \*(AqBase\*(Aq;
418 \& sub Init {
419 \& my ($argsref) = @_;
420 \& ...
421 \& }
422 \&
423 \& package Derived2;
424 \& use base \*(AqBase\*(Aq, \*(AqDerived1\*(Aq;
425 \& sub Init {
426 \& my ($argsref) = @_;
427 \& ...
428 \& }
429 .Ve
430 .PP
431 et cetera. Every derived class than needs some additional initialization
432 behaviour simply adds its own \f(CW\*(C`Init\*(C'\fR method (\fInot\fR a \f(CW\*(C`new\*(C'\fR method),
433 which the call to \f(CW\*(C`EVERY::LAST::Init\*(C'\fR in the inherited constructor
434 then correctly picks up.
435 .SH "SEE ALSO"
436 .IX Header "SEE ALSO"
437 mro
438 (in particular next::method <https://metacpan.org/pod/mro#next::method>),
439 which has been a core module since Perl 5.9.5.
440 .SH AUTHOR
441 .IX Header "AUTHOR"
442 Damian Conway ([email protected])
443 .SH "BUGS AND IRRITATIONS"
444 .IX Header "BUGS AND IRRITATIONS"
445 Because it's a module, not an integral part of the interpreter, \f(CW\*(C`NEXT\*(C'\fR
446 has to guess where the surrounding call was found in the method
447 look-up sequence. In the presence of diamond inheritance patterns
448 it occasionally guesses wrong.
449 .PP
450 It's also too slow (despite caching).
451 .PP
452 Comment, suggestions, and patches welcome.
453 .SH COPYRIGHT
454 .IX Header "COPYRIGHT"
455 .Vb 3
456 \& Copyright (c) 2000\-2001, Damian Conway. All Rights Reserved.
457 \& This module is free software. It may be used, redistributed
458 \& and/or modified under the same terms as Perl itself.
459 .Ve