Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "NEXT 3" | |
132 | .TH NEXT 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | NEXT.pm \- Provide a pseudo\-class NEXT that allows method redispatch | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& use NEXT; | |
139 | .Ve | |
140 | .PP | |
141 | .Vb 3 | |
142 | \& package A; | |
143 | \& sub A::method { print "$_[0]: A method\en"; $_[0]->NEXT::method() } | |
144 | \& sub A::DESTROY { print "$_[0]: A dtor\en"; $_[0]->NEXT::DESTROY() } | |
145 | .Ve | |
146 | .PP | |
147 | .Vb 4 | |
148 | \& package B; | |
149 | \& use base qw( A ); | |
150 | \& sub B::AUTOLOAD { print "$_[0]: B AUTOLOAD\en"; $_[0]->NEXT::AUTOLOAD() } | |
151 | \& sub B::DESTROY { print "$_[0]: B dtor\en"; $_[0]->NEXT::DESTROY() } | |
152 | .Ve | |
153 | .PP | |
154 | .Vb 4 | |
155 | \& package C; | |
156 | \& sub C::method { print "$_[0]: C method\en"; $_[0]->NEXT::method() } | |
157 | \& sub C::AUTOLOAD { print "$_[0]: C AUTOLOAD\en"; $_[0]->NEXT::AUTOLOAD() } | |
158 | \& sub C::DESTROY { print "$_[0]: C dtor\en"; $_[0]->NEXT::DESTROY() } | |
159 | .Ve | |
160 | .PP | |
161 | .Vb 5 | |
162 | \& package D; | |
163 | \& use base qw( B C ); | |
164 | \& sub D::method { print "$_[0]: D method\en"; $_[0]->NEXT::method() } | |
165 | \& sub D::AUTOLOAD { print "$_[0]: D AUTOLOAD\en"; $_[0]->NEXT::AUTOLOAD() } | |
166 | \& sub D::DESTROY { print "$_[0]: D dtor\en"; $_[0]->NEXT::DESTROY() } | |
167 | .Ve | |
168 | .PP | |
169 | .Vb 1 | |
170 | \& package main; | |
171 | .Ve | |
172 | .PP | |
173 | .Vb 1 | |
174 | \& my $obj = bless {}, "D"; | |
175 | .Ve | |
176 | .PP | |
177 | .Vb 2 | |
178 | \& $obj->method(); # Calls D::method, A::method, C::method | |
179 | \& $obj->missing_method(); # Calls D::AUTOLOAD, B::AUTOLOAD, C::AUTOLOAD | |
180 | .Ve | |
181 | .PP | |
182 | .Vb 1 | |
183 | \& # Clean-up calls D::DESTROY, B::DESTROY, A::DESTROY, C::DESTROY | |
184 | .Ve | |
185 | .SH "DESCRIPTION" | |
186 | .IX Header "DESCRIPTION" | |
187 | \&\s-1NEXT\s0.pm adds a pseudoclass named \f(CW\*(C`NEXT\*(C'\fR to any program | |
188 | that uses it. If a method \f(CW\*(C`m\*(C'\fR calls \f(CW\*(C`$self\-\*(C'\fR\fINEXT::m()\fR>, the call to | |
189 | \&\f(CW\*(C`m\*(C'\fR is redispatched as if the calling method had not originally been found. | |
190 | .PP | |
191 | In other words, a call to \f(CW\*(C`$self\-\*(C'\fR\fINEXT::m()\fR> resumes the depth\-first, | |
192 | left-to-right search of \f(CW$self\fR's class hierarchy that resulted in the | |
193 | original call to \f(CW\*(C`m\*(C'\fR. | |
194 | .PP | |
195 | Note that this is not the same thing as \f(CW\*(C`$self\-\*(C'\fR\fISUPER::m()\fR>, which | |
196 | begins a new dispatch that is restricted to searching the ancestors | |
197 | of the current class. \f(CW\*(C`$self\-\*(C'\fR\fINEXT::m()\fR> can backtrack | |
198 | past the current class \*(-- to look for a suitable method in other | |
199 | ancestors of \f(CW$self\fR \*(-- whereas \f(CW\*(C`$self\-\*(C'\fR\fISUPER::m()\fR> cannot. | |
200 | .PP | |
201 | A typical use would be in the destructors of a class hierarchy, | |
202 | as illustrated in the synopsis above. Each class in the hierarchy | |
203 | has a \s-1DESTROY\s0 method that performs some class-specific action | |
204 | and then redispatches the call up the hierarchy. As a result, | |
205 | when an object of class D is destroyed, the destructors of \fIall\fR | |
206 | its parent classes are called (in depth\-first, left-to-right order). | |
207 | .PP | |
208 | Another typical use of redispatch would be in \f(CW\*(C`AUTOLOAD\*(C'\fR'ed methods. | |
209 | If such a method determined that it was not able to handle a | |
210 | particular call, it might choose to redispatch that call, in the | |
211 | hope that some other \f(CW\*(C`AUTOLOAD\*(C'\fR (above it, or to its left) might | |
212 | do better. | |
213 | .PP | |
214 | By default, if a redispatch attempt fails to find another method | |
215 | elsewhere in the objects class hierarchy, it quietly gives up and does | |
216 | nothing (but see \*(L"Enforcing redispatch\*(R"). This gracious acquiesence | |
217 | is also unlike the (generally annoying) behaviour of \f(CW\*(C`SUPER\*(C'\fR, which | |
218 | throws an exception if it cannot redispatch. | |
219 | .PP | |
220 | Note that it is a fatal error for any method (including \f(CW\*(C`AUTOLOAD\*(C'\fR) | |
221 | to attempt to redispatch any method that does not have the | |
222 | same name. For example: | |
223 | .PP | |
224 | .Vb 1 | |
225 | \& sub D::oops { print "oops!\en"; $_[0]->NEXT::other_method() } | |
226 | .Ve | |
227 | .Sh "Enforcing redispatch" | |
228 | .IX Subsection "Enforcing redispatch" | |
229 | It is possible to make \f(CW\*(C`NEXT\*(C'\fR redispatch more demandingly (i.e. like | |
230 | \&\f(CW\*(C`SUPER\*(C'\fR does), so that the redispatch throws an exception if it cannot | |
231 | find a \*(L"next\*(R" method to call. | |
232 | .PP | |
233 | To do this, simple invoke the redispatch as: | |
234 | .PP | |
235 | .Vb 1 | |
236 | \& $self->NEXT::ACTUAL::method(); | |
237 | .Ve | |
238 | .PP | |
239 | rather than: | |
240 | .PP | |
241 | .Vb 1 | |
242 | \& $self->NEXT::method(); | |
243 | .Ve | |
244 | .PP | |
245 | The \f(CW\*(C`ACTUAL\*(C'\fR tells \f(CW\*(C`NEXT\*(C'\fR that there must actually be a next method to call, | |
246 | or it should throw an exception. | |
247 | .PP | |
248 | \&\f(CW\*(C`NEXT::ACTUAL\*(C'\fR is most commonly used in \f(CW\*(C`AUTOLOAD\*(C'\fR methods, as a means to | |
249 | decline an \f(CW\*(C`AUTOLOAD\*(C'\fR request, but preserve the normal exception-on-failure | |
250 | semantics: | |
251 | .PP | |
252 | .Vb 8 | |
253 | \& sub AUTOLOAD { | |
254 | \& if ($AUTOLOAD =~ /foo|bar/) { | |
255 | \& # handle here | |
256 | \& } | |
257 | \& else { # try elsewhere | |
258 | \& shift()->NEXT::ACTUAL::AUTOLOAD(@_); | |
259 | \& } | |
260 | \& } | |
261 | .Ve | |
262 | .PP | |
263 | By using \f(CW\*(C`NEXT::ACTUAL\*(C'\fR, if there is no other \f(CW\*(C`AUTOLOAD\*(C'\fR to handle the | |
264 | method call, an exception will be thrown (as usually happens in the absence of | |
265 | a suitable \f(CW\*(C`AUTOLOAD\*(C'\fR). | |
266 | .Sh "Avoiding repetitions" | |
267 | .IX Subsection "Avoiding repetitions" | |
268 | If \f(CW\*(C`NEXT\*(C'\fR redispatching is used in the methods of a \*(L"diamond\*(R" class hierarchy: | |
269 | .PP | |
270 | .Vb 5 | |
271 | \& # A B | |
272 | \& # / \e / | |
273 | \& # C D | |
274 | \& # \e / | |
275 | \& # E | |
276 | .Ve | |
277 | .PP | |
278 | .Vb 1 | |
279 | \& use NEXT; | |
280 | .Ve | |
281 | .PP | |
282 | .Vb 2 | |
283 | \& package A; | |
284 | \& sub foo { print "called A::foo\en"; shift->NEXT::foo() } | |
285 | .Ve | |
286 | .PP | |
287 | .Vb 2 | |
288 | \& package B; | |
289 | \& sub foo { print "called B::foo\en"; shift->NEXT::foo() } | |
290 | .Ve | |
291 | .PP | |
292 | .Vb 2 | |
293 | \& package C; @ISA = qw( A ); | |
294 | \& sub foo { print "called C::foo\en"; shift->NEXT::foo() } | |
295 | .Ve | |
296 | .PP | |
297 | .Vb 2 | |
298 | \& package D; @ISA = qw(A B); | |
299 | \& sub foo { print "called D::foo\en"; shift->NEXT::foo() } | |
300 | .Ve | |
301 | .PP | |
302 | .Vb 2 | |
303 | \& package E; @ISA = qw(C D); | |
304 | \& sub foo { print "called E::foo\en"; shift->NEXT::foo() } | |
305 | .Ve | |
306 | .PP | |
307 | .Vb 1 | |
308 | \& E->foo(); | |
309 | .Ve | |
310 | .PP | |
311 | then derived classes may (re\-)inherit base-class methods through two or | |
312 | more distinct paths (e.g. in the way \f(CW\*(C`E\*(C'\fR inherits \f(CW\*(C`A::foo\*(C'\fR twice \*(-- | |
313 | 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 | |
314 | will invoke the multiply inherited method as many times as it is | |
315 | inherited. For example, the above code prints: | |
316 | .PP | |
317 | .Vb 6 | |
318 | \& called E::foo | |
319 | \& called C::foo | |
320 | \& called A::foo | |
321 | \& called D::foo | |
322 | \& called A::foo | |
323 | \& called B::foo | |
324 | .Ve | |
325 | .PP | |
326 | (i.e. \f(CW\*(C`A::foo\*(C'\fR is called twice). | |
327 | .PP | |
328 | In some cases this \fImay\fR be the desired effect within a diamond hierarchy, | |
329 | but in others (e.g. for destructors) it may be more appropriate to | |
330 | call each method only once during a sequence of redispatches. | |
331 | .PP | |
332 | To cover such cases, you can redispatch methods via: | |
333 | .PP | |
334 | .Vb 1 | |
335 | \& $self->NEXT::UNSEEN::method(); | |
336 | .Ve | |
337 | .PP | |
338 | rather than: | |
339 | .PP | |
340 | .Vb 1 | |
341 | \& $self->NEXT::method(); | |
342 | .Ve | |
343 | .PP | |
344 | This causes the redispatcher to skip any classes in the hierarchy that it has | |
345 | already visited in an earlier redispatch. So, for example, if the | |
346 | previous example were rewritten: | |
347 | .PP | |
348 | .Vb 2 | |
349 | \& package A; | |
350 | \& sub foo { print "called A::foo\en"; shift->NEXT::UNSEEN::foo() } | |
351 | .Ve | |
352 | .PP | |
353 | .Vb 2 | |
354 | \& package B; | |
355 | \& sub foo { print "called B::foo\en"; shift->NEXT::UNSEEN::foo() } | |
356 | .Ve | |
357 | .PP | |
358 | .Vb 2 | |
359 | \& package C; @ISA = qw( A ); | |
360 | \& sub foo { print "called C::foo\en"; shift->NEXT::UNSEEN::foo() } | |
361 | .Ve | |
362 | .PP | |
363 | .Vb 2 | |
364 | \& package D; @ISA = qw(A B); | |
365 | \& sub foo { print "called D::foo\en"; shift->NEXT::UNSEEN::foo() } | |
366 | .Ve | |
367 | .PP | |
368 | .Vb 2 | |
369 | \& package E; @ISA = qw(C D); | |
370 | \& sub foo { print "called E::foo\en"; shift->NEXT::UNSEEN::foo() } | |
371 | .Ve | |
372 | .PP | |
373 | .Vb 1 | |
374 | \& E->foo(); | |
375 | .Ve | |
376 | .PP | |
377 | then it would print: | |
378 | .PP | |
379 | .Vb 5 | |
380 | \& called E::foo | |
381 | \& called C::foo | |
382 | \& called A::foo | |
383 | \& called D::foo | |
384 | \& called B::foo | |
385 | .Ve | |
386 | .PP | |
387 | and omit the second call to \f(CW\*(C`A::foo\*(C'\fR. | |
388 | .PP | |
389 | Note that you can also use: | |
390 | .PP | |
391 | .Vb 1 | |
392 | \& $self->NEXT::UNSEEN::ACTUAL::method(); | |
393 | .Ve | |
394 | .PP | |
395 | or: | |
396 | .PP | |
397 | .Vb 1 | |
398 | \& $self->NEXT::ACTUAL::UNSEEN::method(); | |
399 | .Ve | |
400 | .PP | |
401 | to get both unique invocation \fIand\fR exception\-on\-failure. | |
402 | .SH "AUTHOR" | |
403 | .IX Header "AUTHOR" | |
404 | Damian Conway (damian@conway.org) | |
405 | .SH "BUGS AND IRRITATIONS" | |
406 | .IX Header "BUGS AND IRRITATIONS" | |
407 | Because it's a module, not an integral part of the interpreter, \s-1NEXT\s0.pm | |
408 | has to guess where the surrounding call was found in the method | |
409 | look-up sequence. In the presence of diamond inheritance patterns | |
410 | it occasionally guesses wrong. | |
411 | .PP | |
412 | It's also too slow (despite caching). | |
413 | .PP | |
414 | Comment, suggestions, and patches welcome. | |
415 | .SH "COPYRIGHT" | |
416 | .IX Header "COPYRIGHT" | |
417 | .Vb 3 | |
418 | \& Copyright (c) 2000-2001, Damian Conway. All Rights Reserved. | |
419 | \& This module is free software. It may be used, redistributed | |
420 | \& and/or modified under the same terms as Perl itself. | |
421 | .Ve |