6 sub NEXT::ELSEWHERE::ancestors
10 while (my $next = shift @inlist) {
13 unshift @inlist, @{"$outlist[-1]::ISA"};
18 sub NEXT::ELSEWHERE::ordered_ancestors
22 while (my $next = shift @inlist) {
25 push @inlist, @{"$outlist[-1]::ISA"};
27 return sort { $a->isa($b) ? -1
36 until ((caller($depth))[3] !~ /^\(eval\)$/) { $depth++ }
37 my $caller = (caller($depth))[3];
38 my $wanted = $NEXT::AUTOLOAD || 'NEXT::AUTOLOAD';
39 undef $NEXT::AUTOLOAD;
40 my ($caller_class, $caller_method) = $caller =~ m{(.*)::(.*)}g;
41 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
42 croak "Can't call $wanted from $caller"
43 unless $caller_method eq $wanted_method;
45 local ($NEXT::NEXT{$self,$wanted_method}, $NEXT::SEEN) =
46 ($NEXT::NEXT{$self,$wanted_method}, $NEXT::SEEN);
49 unless ($NEXT::NEXT{$self,$wanted_method}) {
51 NEXT::ELSEWHERE::ancestors ref $self || $self,
54 last if shift @forebears eq $caller_class
57 @{$NEXT::NEXT{$self,$wanted_method}} =
58 map { *{"${_}::$caller_method"}{CODE}||() } @forebears
59 unless $wanted_method eq 'AUTOLOAD';
60 @{$NEXT::NEXT{$self,$wanted_method}} =
61 map { (*{"${_}::AUTOLOAD"}{CODE}) ? "${_}::AUTOLOAD" : ()} @forebears
62 unless @{$NEXT::NEXT{$self,$wanted_method}||[]};
63 $NEXT::SEEN->{$self,*{$caller}{CODE}}++;
65 my $call_method = shift @{$NEXT::NEXT{$self,$wanted_method}};
66 while ($wanted_class =~ /^NEXT\b.*\b(UNSEEN|DISTINCT)\b/
67 && defined $call_method
68 && $NEXT::SEEN->{$self,$call_method}++) {
69 $call_method = shift @{$NEXT::NEXT{$self,$wanted_method}};
71 unless (defined $call_method) {
72 return unless $wanted_class =~ /^NEXT:.*:ACTUAL/;
73 (local $Carp::CarpLevel)++;
74 croak qq(Can't locate object method "$wanted_method" ),
75 qq(via package "$caller_class");
77 return $self->$call_method(@_[1..$#_]) if ref $call_method eq 'CODE';
79 ($wanted_method=${$caller_class."::AUTOLOAD"}) =~ s/.*:://
80 if $wanted_method eq 'AUTOLOAD';
81 $$call_method = $caller_class."::NEXT::".$wanted_method;
82 return $call_method->(@_);
86 package NEXT::UNSEEN; @ISA = 'NEXT';
87 package NEXT::DISTINCT; @ISA = 'NEXT';
88 package NEXT::ACTUAL; @ISA = 'NEXT';
89 package NEXT::ACTUAL::UNSEEN; @ISA = 'NEXT';
90 package NEXT::ACTUAL::DISTINCT; @ISA = 'NEXT';
91 package NEXT::UNSEEN::ACTUAL; @ISA = 'NEXT';
92 package NEXT::DISTINCT::ACTUAL; @ISA = 'NEXT';
94 package EVERY::LAST; @ISA = 'EVERY';
95 package EVERY; @ISA = 'NEXT';
100 until ((caller($depth))[3] !~ /^\(eval\)$/) { $depth++ }
101 my $caller = (caller($depth))[3];
102 my $wanted = $EVERY::AUTOLOAD || 'EVERY::AUTOLOAD';
103 undef $EVERY::AUTOLOAD;
104 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
106 local $NEXT::ALREADY_IN_EVERY{$self,$wanted_method} =
107 $NEXT::ALREADY_IN_EVERY{$self,$wanted_method};
109 return if $NEXT::ALREADY_IN_EVERY{$self,$wanted_method}++;
111 my @forebears = NEXT::ELSEWHERE::ordered_ancestors ref $self || $self,
113 @forebears = reverse @forebears if $wanted_class =~ /\bLAST\b/;
116 my @every = map { my $sub = "${_}::$wanted_method";
117 !*{$sub}{CODE} || $seen{$sub}++ ? () : $sub
119 unless $wanted_method eq 'AUTOLOAD';
121 my $want = wantarray;
124 return map {($_, [$self->$_(@_[1..$#_])])} @every;
126 elsif (defined $want) {
127 return { map {($_, scalar($self->$_(@_[1..$#_])))}
132 $self->$_(@_[1..$#_]) for @every;
137 @every = map { my $sub = "${_}::AUTOLOAD";
138 !*{$sub}{CODE} || $seen{$sub}++ ? () : "${_}::AUTOLOAD"
141 return map { $$_ = ref($self)."::EVERY::".$wanted_method;
142 ($_, [$self->$_(@_[1..$#_])]);
145 elsif (defined $want) {
146 return { map { $$_ = ref($self)."::EVERY::".$wanted_method;
147 ($_, scalar($self->$_(@_[1..$#_])))
153 $$_ = ref($self)."::EVERY::".$wanted_method;
154 $self->$_(@_[1..$#_]);
167 NEXT.pm - Provide a pseudo-class NEXT (et al) that allows method redispatch
175 sub A::method { print "$_[0]: A method\n"; $_[0]->NEXT::method() }
176 sub A::DESTROY { print "$_[0]: A dtor\n"; $_[0]->NEXT::DESTROY() }
180 sub B::AUTOLOAD { print "$_[0]: B AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
181 sub B::DESTROY { print "$_[0]: B dtor\n"; $_[0]->NEXT::DESTROY() }
184 sub C::method { print "$_[0]: C method\n"; $_[0]->NEXT::method() }
185 sub C::AUTOLOAD { print "$_[0]: C AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
186 sub C::DESTROY { print "$_[0]: C dtor\n"; $_[0]->NEXT::DESTROY() }
190 sub D::method { print "$_[0]: D method\n"; $_[0]->NEXT::method() }
191 sub D::AUTOLOAD { print "$_[0]: D AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
192 sub D::DESTROY { print "$_[0]: D dtor\n"; $_[0]->NEXT::DESTROY() }
196 my $obj = bless {}, "D";
198 $obj->method(); # Calls D::method, A::method, C::method
199 $obj->missing_method(); # Calls D::AUTOLOAD, B::AUTOLOAD, C::AUTOLOAD
201 # Clean-up calls D::DESTROY, B::DESTROY, A::DESTROY, C::DESTROY
207 NEXT.pm adds a pseudoclass named C<NEXT> to any program
208 that uses it. If a method C<m> calls C<$self-E<gt>NEXT::m()>, the call to
209 C<m> is redispatched as if the calling method had not originally been found.
211 In other words, a call to C<$self-E<gt>NEXT::m()> resumes the depth-first,
212 left-to-right search of C<$self>'s class hierarchy that resulted in the
213 original call to C<m>.
215 Note that this is not the same thing as C<$self-E<gt>SUPER::m()>, which
216 begins a new dispatch that is restricted to searching the ancestors
217 of the current class. C<$self-E<gt>NEXT::m()> can backtrack
218 past the current class -- to look for a suitable method in other
219 ancestors of C<$self> -- whereas C<$self-E<gt>SUPER::m()> cannot.
221 A typical use would be in the destructors of a class hierarchy,
222 as illustrated in the synopsis above. Each class in the hierarchy
223 has a DESTROY method that performs some class-specific action
224 and then redispatches the call up the hierarchy. As a result,
225 when an object of class D is destroyed, the destructors of I<all>
226 its parent classes are called (in depth-first, left-to-right order).
228 Another typical use of redispatch would be in C<AUTOLOAD>'ed methods.
229 If such a method determined that it was not able to handle a
230 particular call, it might choose to redispatch that call, in the
231 hope that some other C<AUTOLOAD> (above it, or to its left) might
234 By default, if a redispatch attempt fails to find another method
235 elsewhere in the objects class hierarchy, it quietly gives up and does
236 nothing (but see L<"Enforcing redispatch">). This gracious acquiesence
237 is also unlike the (generally annoying) behaviour of C<SUPER>, which
238 throws an exception if it cannot redispatch.
240 Note that it is a fatal error for any method (including C<AUTOLOAD>)
241 to attempt to redispatch any method that does not have the
242 same name. For example:
244 sub D::oops { print "oops!\n"; $_[0]->NEXT::other_method() }
247 =head2 Enforcing redispatch
249 It is possible to make C<NEXT> redispatch more demandingly (i.e. like
250 C<SUPER> does), so that the redispatch throws an exception if it cannot
251 find a "next" method to call.
253 To do this, simple invoke the redispatch as:
255 $self->NEXT::ACTUAL::method();
259 $self->NEXT::method();
261 The C<ACTUAL> tells C<NEXT> that there must actually be a next method to call,
262 or it should throw an exception.
264 C<NEXT::ACTUAL> is most commonly used in C<AUTOLOAD> methods, as a means to
265 decline an C<AUTOLOAD> request, but preserve the normal exception-on-failure
269 if ($AUTOLOAD =~ /foo|bar/) {
272 else { # try elsewhere
273 shift()->NEXT::ACTUAL::AUTOLOAD(@_);
277 By using C<NEXT::ACTUAL>, if there is no other C<AUTOLOAD> to handle the
278 method call, an exception will be thrown (as usually happens in the absence of
279 a suitable C<AUTOLOAD>).
282 =head2 Avoiding repetitions
284 If C<NEXT> redispatching is used in the methods of a "diamond" class hierarchy:
295 sub foo { print "called A::foo\n"; shift->NEXT::foo() }
298 sub foo { print "called B::foo\n"; shift->NEXT::foo() }
300 package C; @ISA = qw( A );
301 sub foo { print "called C::foo\n"; shift->NEXT::foo() }
303 package D; @ISA = qw(A B);
304 sub foo { print "called D::foo\n"; shift->NEXT::foo() }
306 package E; @ISA = qw(C D);
307 sub foo { print "called E::foo\n"; shift->NEXT::foo() }
311 then derived classes may (re-)inherit base-class methods through two or
312 more distinct paths (e.g. in the way C<E> inherits C<A::foo> twice --
313 through C<C> and C<D>). In such cases, a sequence of C<NEXT> redispatches
314 will invoke the multiply inherited method as many times as it is
315 inherited. For example, the above code prints:
324 (i.e. C<A::foo> is called twice).
326 In some cases this I<may> be the desired effect within a diamond hierarchy,
327 but in others (e.g. for destructors) it may be more appropriate to
328 call each method only once during a sequence of redispatches.
330 To cover such cases, you can redispatch methods via:
332 $self->NEXT::DISTINCT::method();
336 $self->NEXT::method();
338 This causes the redispatcher to only visit each distinct C<method> method
339 once. That is, to skip any classes in the hierarchy that it has
340 already visited during redispatch. So, for example, if the
341 previous example were rewritten:
344 sub foo { print "called A::foo\n"; shift->NEXT::DISTINCT::foo() }
347 sub foo { print "called B::foo\n"; shift->NEXT::DISTINCT::foo() }
349 package C; @ISA = qw( A );
350 sub foo { print "called C::foo\n"; shift->NEXT::DISTINCT::foo() }
352 package D; @ISA = qw(A B);
353 sub foo { print "called D::foo\n"; shift->NEXT::DISTINCT::foo() }
355 package E; @ISA = qw(C D);
356 sub foo { print "called E::foo\n"; shift->NEXT::DISTINCT::foo() }
368 and omit the second call to C<A::foo> (since it would not be distinct
369 from the first call to C<A::foo>).
371 Note that you can also use:
373 $self->NEXT::DISTINCT::ACTUAL::method();
377 $self->NEXT::ACTUAL::DISTINCT::method();
379 to get both unique invocation I<and> exception-on-failure.
381 Note that, for historical compatibility, you can also use
382 C<NEXT::UNSEEN> instead of C<NEXT::DISTINCT>.
385 =head2 Invoking all versions of a method with a single call
387 Yet another pseudo-class that NEXT.pm provides is C<EVERY>.
388 Its behaviour is considerably simpler than that of the C<NEXT> family.
393 calls I<every> method named C<foo> that the object in C<$obj> has inherited.
398 package A; @ISA = qw(B D X);
399 sub foo { print "A::foo " }
401 package B; @ISA = qw(D X);
402 sub foo { print "B::foo " }
404 package X; @ISA = qw(D);
405 sub foo { print "X::foo " }
408 sub foo { print "D::foo " }
412 my $obj = bless {}, 'A';
413 $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
415 Prefixing a method call with C<EVERY::> causes every method in the
416 object's hierarchy with that name to be invoked. As the above example
417 illustrates, they are not called in Perl's usual "left-most-depth-first"
418 order. Instead, they are called "breadth-first-dependency-wise".
420 That means that the inheritance tree of the object is traversed breadth-first
421 and the resulting order of classes is used as the sequence in which methods
422 are called. However, that sequence is modified by imposing a rule that the
423 appropritae method of a derived class must be called before the same method of
424 any ancestral class. That's why, in the above example, C<X::foo> is called
425 before C<D::foo>, even though C<D> comes before C<X> in C<@B::ISA>.
427 In general, there's no need to worry about the order of calls. They will be
428 left-to-right, breadth-first, most-derived-first. This works perfectly for
429 most inherited methods (including destructors), but is inappropriate for
430 some kinds of methods (such as constructors, cloners, debuggers, and
431 initializers) where it's more appropriate that the least-derived methods be
432 called first (as more-derived methods may rely on the behaviour of their
433 "ancestors"). In that case, instead of using the C<EVERY> pseudo-class:
435 $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
437 you can use the C<EVERY::LAST> pseudo-class:
439 $obj->EVERY::LAST::foo(); # prints" D::foo X::foo B::foo A::foo
441 which reverses the order of method call.
443 Whichever version is used, the actual methods are called in the same
444 context (list, scalar, or void) as the original call via C<EVERY>, and return:
450 A hash of array references in list context. Each entry of the hash has the
451 fully qualified method name as its key and a reference to an array containing
452 the method's list-context return values as its value.
456 A reference to a hash of scalar values in scalar context. Each entry of the hash has the
457 fully qualified method name as its key and the method's scalar-context return values as its value.
461 Nothing in void context (obviously).
465 =head2 Using C<EVERY> methods
467 The typical way to use an C<EVERY> call is to wrap it in another base
468 method, that all classes inherit. For example, to ensure that every
469 destructor an object inherits is actually called (as opposed to just the
470 left-most-depth-first-est one):
473 sub DESTROY { $_[0]->EVERY::Destroy }
480 use base 'Base', 'Derived1';
483 et cetera. Every derived class than needs its own clean-up
484 behaviour simply adds its own C<Destroy> method (I<not> a C<DESTROY> method),
485 which the call to C<EVERY::LAST::Destroy> in the inherited destructor
486 then correctly picks up.
488 Likewise, to create a class hierarchy in which every initializer inherited by
489 a new object is invoked:
493 my ($class, %args) = @_;
494 my $obj = bless {}, $class;
495 $obj->EVERY::LAST::Init(\%args);
506 use base 'Base', 'Derived1';
512 et cetera. Every derived class than needs some additional initialization
513 behaviour simply adds its own C<Init> method (I<not> a C<new> method),
514 which the call to C<EVERY::LAST::Init> in the inherited constructor
515 then correctly picks up.
520 Damian Conway (damian@conway.org)
522 =head1 BUGS AND IRRITATIONS
524 Because it's a module, not an integral part of the interpreter, NEXT.pm
525 has to guess where the surrounding call was found in the method
526 look-up sequence. In the presence of diamond inheritance patterns
527 it occasionally guesses wrong.
529 It's also too slow (despite caching).
531 Comment, suggestions, and patches welcome.
535 Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
536 This module is free software. It may be used, redistributed
537 and/or modified under the same terms as Perl itself.