Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
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 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | NEXT.pm \- Provide a pseudo\-class NEXT (et al) 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\->NEXT::m()\*(C'\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\->NEXT::m()\*(C'\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\->SUPER::m()\*(C'\fR, which | |
196 | begins a new dispatch that is restricted to searching the ancestors | |
197 | of the current class. \f(CW\*(C`$self\->NEXT::m()\*(C'\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\->SUPER::m()\*(C'\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::DISTINCT::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 only visit each distinct \f(CW\*(C`method\*(C'\fR method | |
345 | once. That is, to skip any classes in the hierarchy that it has | |
346 | already visited during redispatch. So, for example, if the | |
347 | previous example were rewritten: | |
348 | .PP | |
349 | .Vb 2 | |
350 | \& package A; | |
351 | \& sub foo { print "called A::foo\en"; shift->NEXT::DISTINCT::foo() } | |
352 | .Ve | |
353 | .PP | |
354 | .Vb 2 | |
355 | \& package B; | |
356 | \& sub foo { print "called B::foo\en"; shift->NEXT::DISTINCT::foo() } | |
357 | .Ve | |
358 | .PP | |
359 | .Vb 2 | |
360 | \& package C; @ISA = qw( A ); | |
361 | \& sub foo { print "called C::foo\en"; shift->NEXT::DISTINCT::foo() } | |
362 | .Ve | |
363 | .PP | |
364 | .Vb 2 | |
365 | \& package D; @ISA = qw(A B); | |
366 | \& sub foo { print "called D::foo\en"; shift->NEXT::DISTINCT::foo() } | |
367 | .Ve | |
368 | .PP | |
369 | .Vb 2 | |
370 | \& package E; @ISA = qw(C D); | |
371 | \& sub foo { print "called E::foo\en"; shift->NEXT::DISTINCT::foo() } | |
372 | .Ve | |
373 | .PP | |
374 | .Vb 1 | |
375 | \& E->foo(); | |
376 | .Ve | |
377 | .PP | |
378 | then it would print: | |
379 | .PP | |
380 | .Vb 5 | |
381 | \& called E::foo | |
382 | \& called C::foo | |
383 | \& called A::foo | |
384 | \& called D::foo | |
385 | \& called B::foo | |
386 | .Ve | |
387 | .PP | |
388 | and omit the second call to \f(CW\*(C`A::foo\*(C'\fR (since it would not be distinct | |
389 | from the first call to \f(CW\*(C`A::foo\*(C'\fR). | |
390 | .PP | |
391 | Note that you can also use: | |
392 | .PP | |
393 | .Vb 1 | |
394 | \& $self->NEXT::DISTINCT::ACTUAL::method(); | |
395 | .Ve | |
396 | .PP | |
397 | or: | |
398 | .PP | |
399 | .Vb 1 | |
400 | \& $self->NEXT::ACTUAL::DISTINCT::method(); | |
401 | .Ve | |
402 | .PP | |
403 | to get both unique invocation \fIand\fR exception\-on\-failure. | |
404 | .PP | |
405 | Note that, for historical compatibility, you can also use | |
406 | \&\f(CW\*(C`NEXT::UNSEEN\*(C'\fR instead of \f(CW\*(C`NEXT::DISTINCT\*(C'\fR. | |
407 | .Sh "Invoking all versions of a method with a single call" | |
408 | .IX Subsection "Invoking all versions of a method with a single call" | |
409 | Yet another pseudo-class that \s-1NEXT\s0.pm provides is \f(CW\*(C`EVERY\*(C'\fR. | |
410 | Its behaviour is considerably simpler than that of the \f(CW\*(C`NEXT\*(C'\fR family. | |
411 | A call to: | |
412 | .PP | |
413 | .Vb 1 | |
414 | \& $obj->EVERY::foo(); | |
415 | .Ve | |
416 | .PP | |
417 | calls \fIevery\fR method named \f(CW\*(C`foo\*(C'\fR that the object in \f(CW$obj\fR has inherited. | |
418 | That is: | |
419 | .PP | |
420 | .Vb 1 | |
421 | \& use NEXT; | |
422 | .Ve | |
423 | .PP | |
424 | .Vb 2 | |
425 | \& package A; @ISA = qw(B D X); | |
426 | \& sub foo { print "A::foo " } | |
427 | .Ve | |
428 | .PP | |
429 | .Vb 2 | |
430 | \& package B; @ISA = qw(D X); | |
431 | \& sub foo { print "B::foo " } | |
432 | .Ve | |
433 | .PP | |
434 | .Vb 2 | |
435 | \& package X; @ISA = qw(D); | |
436 | \& sub foo { print "X::foo " } | |
437 | .Ve | |
438 | .PP | |
439 | .Vb 2 | |
440 | \& package D; | |
441 | \& sub foo { print "D::foo " } | |
442 | .Ve | |
443 | .PP | |
444 | .Vb 1 | |
445 | \& package main; | |
446 | .Ve | |
447 | .PP | |
448 | .Vb 2 | |
449 | \& my $obj = bless {}, 'A'; | |
450 | \& $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo | |
451 | .Ve | |
452 | .PP | |
453 | Prefixing a method call with \f(CW\*(C`EVERY::\*(C'\fR causes every method in the | |
454 | object's hierarchy with that name to be invoked. As the above example | |
455 | illustrates, they are not called in Perl's usual \*(L"left\-most\-depth\-first\*(R" | |
456 | order. Instead, they are called \*(L"breadth\-first\-dependency\-wise\*(R". | |
457 | .PP | |
458 | That means that the inheritance tree of the object is traversed breadth-first | |
459 | and the resulting order of classes is used as the sequence in which methods | |
460 | are called. However, that sequence is modified by imposing a rule that the | |
461 | appropritae method of a derived class must be called before the same method of | |
462 | any ancestral class. That's why, in the above example, \f(CW\*(C`X::foo\*(C'\fR is called | |
463 | 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. | |
464 | .PP | |
465 | In general, there's no need to worry about the order of calls. They will be | |
466 | left\-to\-right, breadth\-first, most\-derived\-first. This works perfectly for | |
467 | most inherited methods (including destructors), but is inappropriate for | |
468 | some kinds of methods (such as constructors, cloners, debuggers, and | |
469 | initializers) where it's more appropriate that the least-derived methods be | |
470 | called first (as more-derived methods may rely on the behaviour of their | |
471 | \&\*(L"ancestors\*(R"). In that case, instead of using the \f(CW\*(C`EVERY\*(C'\fR pseudo\-class: | |
472 | .PP | |
473 | .Vb 1 | |
474 | \& $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo | |
475 | .Ve | |
476 | .PP | |
477 | you can use the \f(CW\*(C`EVERY::LAST\*(C'\fR pseudo\-class: | |
478 | .PP | |
479 | .Vb 1 | |
480 | \& $obj->EVERY::LAST::foo(); # prints" D::foo X::foo B::foo A::foo | |
481 | .Ve | |
482 | .PP | |
483 | which reverses the order of method call. | |
484 | .PP | |
485 | Whichever version is used, the actual methods are called in the same | |
486 | context (list, scalar, or void) as the original call via \f(CW\*(C`EVERY\*(C'\fR, and return: | |
487 | .IP "\(bu" 4 | |
488 | A hash of array references in list context. Each entry of the hash has the | |
489 | fully qualified method name as its key and a reference to an array containing | |
490 | the method's list-context return values as its value. | |
491 | .IP "\(bu" 4 | |
492 | A reference to a hash of scalar values in scalar context. Each entry of the hash has the | |
493 | fully qualified method name as its key and the method's scalar-context return values as its value. | |
494 | .IP "\(bu" 4 | |
495 | Nothing in void context (obviously). | |
496 | .ie n .Sh "Using ""EVERY"" methods" | |
497 | .el .Sh "Using \f(CWEVERY\fP methods" | |
498 | .IX Subsection "Using EVERY methods" | |
499 | The typical way to use an \f(CW\*(C`EVERY\*(C'\fR call is to wrap it in another base | |
500 | method, that all classes inherit. For example, to ensure that every | |
501 | destructor an object inherits is actually called (as opposed to just the | |
502 | left-most-depth-first-est one): | |
503 | .PP | |
504 | .Vb 2 | |
505 | \& package Base; | |
506 | \& sub DESTROY { $_[0]->EVERY::Destroy } | |
507 | .Ve | |
508 | .PP | |
509 | .Vb 3 | |
510 | \& package Derived1; | |
511 | \& use base 'Base'; | |
512 | \& sub Destroy {...} | |
513 | .Ve | |
514 | .PP | |
515 | .Vb 3 | |
516 | \& package Derived2; | |
517 | \& use base 'Base', 'Derived1'; | |
518 | \& sub Destroy {...} | |
519 | .Ve | |
520 | .PP | |
521 | et cetera. Every derived class than needs its own clean-up | |
522 | behaviour simply adds its own \f(CW\*(C`Destroy\*(C'\fR method (\fInot\fR a \f(CW\*(C`DESTROY\*(C'\fR method), | |
523 | which the call to \f(CW\*(C`EVERY::LAST::Destroy\*(C'\fR in the inherited destructor | |
524 | then correctly picks up. | |
525 | .PP | |
526 | Likewise, to create a class hierarchy in which every initializer inherited by | |
527 | a new object is invoked: | |
528 | .PP | |
529 | .Vb 6 | |
530 | \& package Base; | |
531 | \& sub new { | |
532 | \& my ($class, %args) = @_; | |
533 | \& my $obj = bless {}, $class; | |
534 | \& $obj->EVERY::LAST::Init(\e%args); | |
535 | \& } | |
536 | .Ve | |
537 | .PP | |
538 | .Vb 6 | |
539 | \& package Derived1; | |
540 | \& use base 'Base'; | |
541 | \& sub Init { | |
542 | \& my ($argsref) = @_; | |
543 | \& ... | |
544 | \& } | |
545 | .Ve | |
546 | .PP | |
547 | .Vb 6 | |
548 | \& package Derived2; | |
549 | \& use base 'Base', 'Derived1'; | |
550 | \& sub Init { | |
551 | \& my ($argsref) = @_; | |
552 | \& ... | |
553 | \& } | |
554 | .Ve | |
555 | .PP | |
556 | et cetera. Every derived class than needs some additional initialization | |
557 | behaviour simply adds its own \f(CW\*(C`Init\*(C'\fR method (\fInot\fR a \f(CW\*(C`new\*(C'\fR method), | |
558 | which the call to \f(CW\*(C`EVERY::LAST::Init\*(C'\fR in the inherited constructor | |
559 | then correctly picks up. | |
560 | .SH "AUTHOR" | |
561 | .IX Header "AUTHOR" | |
562 | Damian Conway (damian@conway.org) | |
563 | .SH "BUGS AND IRRITATIONS" | |
564 | .IX Header "BUGS AND IRRITATIONS" | |
565 | Because it's a module, not an integral part of the interpreter, \s-1NEXT\s0.pm | |
566 | has to guess where the surrounding call was found in the method | |
567 | look-up sequence. In the presence of diamond inheritance patterns | |
568 | it occasionally guesses wrong. | |
569 | .PP | |
570 | It's also too slow (despite caching). | |
571 | .PP | |
572 | Comment, suggestions, and patches welcome. | |
573 | .SH "COPYRIGHT" | |
574 | .IX Header "COPYRIGHT" | |
575 | .Vb 3 | |
576 | \& Copyright (c) 2000-2001, Damian Conway. All Rights Reserved. | |
577 | \& This module is free software. It may be used, redistributed | |
578 | \& and/or modified under the same terms as Perl itself. | |
579 | .Ve |