Updated to Git v1.8.4
[msysgit.git] / lib / perl5 / 5.8.8 / NEXT.pm
blob64610fed27d3a93954ade6b6c9031f322ef04498
1 package NEXT;
2 $VERSION = '0.60';
3 use Carp;
4 use strict;
6 sub NEXT::ELSEWHERE::ancestors
8 my @inlist = shift;
9 my @outlist = ();
10 while (my $next = shift @inlist) {
11 push @outlist, $next;
12 no strict 'refs';
13 unshift @inlist, @{"$outlist[-1]::ISA"};
15 return @outlist;
18 sub NEXT::ELSEWHERE::ordered_ancestors
20 my @inlist = shift;
21 my @outlist = ();
22 while (my $next = shift @inlist) {
23 push @outlist, $next;
24 no strict 'refs';
25 push @inlist, @{"$outlist[-1]::ISA"};
27 return sort { $a->isa($b) ? -1
28 : $b->isa($a) ? +1
29 : 0 } @outlist;
32 sub AUTOLOAD
34 my ($self) = @_;
35 my $caller = (caller(1))[3];
36 my $wanted = $NEXT::AUTOLOAD || 'NEXT::AUTOLOAD';
37 undef $NEXT::AUTOLOAD;
38 my ($caller_class, $caller_method) = $caller =~ m{(.*)::(.*)}g;
39 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
40 croak "Can't call $wanted from $caller"
41 unless $caller_method eq $wanted_method;
43 local ($NEXT::NEXT{$self,$wanted_method}, $NEXT::SEEN) =
44 ($NEXT::NEXT{$self,$wanted_method}, $NEXT::SEEN);
47 unless ($NEXT::NEXT{$self,$wanted_method}) {
48 my @forebears =
49 NEXT::ELSEWHERE::ancestors ref $self || $self,
50 $wanted_class;
51 while (@forebears) {
52 last if shift @forebears eq $caller_class
54 no strict 'refs';
55 @{$NEXT::NEXT{$self,$wanted_method}} =
56 map { *{"${_}::$caller_method"}{CODE}||() } @forebears
57 unless $wanted_method eq 'AUTOLOAD';
58 @{$NEXT::NEXT{$self,$wanted_method}} =
59 map { (*{"${_}::AUTOLOAD"}{CODE}) ? "${_}::AUTOLOAD" : ()} @forebears
60 unless @{$NEXT::NEXT{$self,$wanted_method}||[]};
61 $NEXT::SEEN->{$self,*{$caller}{CODE}}++;
63 my $call_method = shift @{$NEXT::NEXT{$self,$wanted_method}};
64 while ($wanted_class =~ /^NEXT\b.*\b(UNSEEN|DISTINCT)\b/
65 && defined $call_method
66 && $NEXT::SEEN->{$self,$call_method}++) {
67 $call_method = shift @{$NEXT::NEXT{$self,$wanted_method}};
69 unless (defined $call_method) {
70 return unless $wanted_class =~ /^NEXT:.*:ACTUAL/;
71 (local $Carp::CarpLevel)++;
72 croak qq(Can't locate object method "$wanted_method" ),
73 qq(via package "$caller_class");
75 return $self->$call_method(@_[1..$#_]) if ref $call_method eq 'CODE';
76 no strict 'refs';
77 ($wanted_method=${$caller_class."::AUTOLOAD"}) =~ s/.*:://
78 if $wanted_method eq 'AUTOLOAD';
79 $$call_method = $caller_class."::NEXT::".$wanted_method;
80 return $call_method->(@_);
83 no strict 'vars';
84 package NEXT::UNSEEN; @ISA = 'NEXT';
85 package NEXT::DISTINCT; @ISA = 'NEXT';
86 package NEXT::ACTUAL; @ISA = 'NEXT';
87 package NEXT::ACTUAL::UNSEEN; @ISA = 'NEXT';
88 package NEXT::ACTUAL::DISTINCT; @ISA = 'NEXT';
89 package NEXT::UNSEEN::ACTUAL; @ISA = 'NEXT';
90 package NEXT::DISTINCT::ACTUAL; @ISA = 'NEXT';
92 package EVERY::LAST; @ISA = 'EVERY';
93 package EVERY; @ISA = 'NEXT';
94 sub AUTOLOAD
96 my ($self) = @_;
97 my $caller = (caller(1))[3];
98 my $wanted = $EVERY::AUTOLOAD || 'EVERY::AUTOLOAD';
99 undef $EVERY::AUTOLOAD;
100 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
102 local $NEXT::ALREADY_IN_EVERY{$self,$wanted_method} =
103 $NEXT::ALREADY_IN_EVERY{$self,$wanted_method};
105 return if $NEXT::ALREADY_IN_EVERY{$self,$wanted_method}++;
107 my @forebears = NEXT::ELSEWHERE::ordered_ancestors ref $self || $self,
108 $wanted_class;
109 @forebears = reverse @forebears if $wanted_class =~ /\bLAST\b/;
110 no strict 'refs';
111 my %seen;
112 my @every = map { my $sub = "${_}::$wanted_method";
113 !*{$sub}{CODE} || $seen{$sub}++ ? () : $sub
114 } @forebears
115 unless $wanted_method eq 'AUTOLOAD';
117 my $want = wantarray;
118 if (@every) {
119 if ($want) {
120 return map {($_, [$self->$_(@_[1..$#_])])} @every;
122 elsif (defined $want) {
123 return { map {($_, scalar($self->$_(@_[1..$#_])))}
124 @every
127 else {
128 $self->$_(@_[1..$#_]) for @every;
129 return;
133 @every = map { my $sub = "${_}::AUTOLOAD";
134 !*{$sub}{CODE} || $seen{$sub}++ ? () : "${_}::AUTOLOAD"
135 } @forebears;
136 if ($want) {
137 return map { $$_ = ref($self)."::EVERY::".$wanted_method;
138 ($_, [$self->$_(@_[1..$#_])]);
139 } @every;
141 elsif (defined $want) {
142 return { map { $$_ = ref($self)."::EVERY::".$wanted_method;
143 ($_, scalar($self->$_(@_[1..$#_])))
144 } @every
147 else {
148 for (@every) {
149 $$_ = ref($self)."::EVERY::".$wanted_method;
150 $self->$_(@_[1..$#_]);
152 return;
159 __END__
161 =head1 NAME
163 NEXT.pm - Provide a pseudo-class NEXT (et al) that allows method redispatch
166 =head1 SYNOPSIS
168 use NEXT;
170 package A;
171 sub A::method { print "$_[0]: A method\n"; $_[0]->NEXT::method() }
172 sub A::DESTROY { print "$_[0]: A dtor\n"; $_[0]->NEXT::DESTROY() }
174 package B;
175 use base qw( A );
176 sub B::AUTOLOAD { print "$_[0]: B AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
177 sub B::DESTROY { print "$_[0]: B dtor\n"; $_[0]->NEXT::DESTROY() }
179 package C;
180 sub C::method { print "$_[0]: C method\n"; $_[0]->NEXT::method() }
181 sub C::AUTOLOAD { print "$_[0]: C AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
182 sub C::DESTROY { print "$_[0]: C dtor\n"; $_[0]->NEXT::DESTROY() }
184 package D;
185 use base qw( B C );
186 sub D::method { print "$_[0]: D method\n"; $_[0]->NEXT::method() }
187 sub D::AUTOLOAD { print "$_[0]: D AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
188 sub D::DESTROY { print "$_[0]: D dtor\n"; $_[0]->NEXT::DESTROY() }
190 package main;
192 my $obj = bless {}, "D";
194 $obj->method(); # Calls D::method, A::method, C::method
195 $obj->missing_method(); # Calls D::AUTOLOAD, B::AUTOLOAD, C::AUTOLOAD
197 # Clean-up calls D::DESTROY, B::DESTROY, A::DESTROY, C::DESTROY
201 =head1 DESCRIPTION
203 NEXT.pm adds a pseudoclass named C<NEXT> to any program
204 that uses it. If a method C<m> calls C<$self-E<gt>NEXT::m()>, the call to
205 C<m> is redispatched as if the calling method had not originally been found.
207 In other words, a call to C<$self-E<gt>NEXT::m()> resumes the depth-first,
208 left-to-right search of C<$self>'s class hierarchy that resulted in the
209 original call to C<m>.
211 Note that this is not the same thing as C<$self-E<gt>SUPER::m()>, which
212 begins a new dispatch that is restricted to searching the ancestors
213 of the current class. C<$self-E<gt>NEXT::m()> can backtrack
214 past the current class -- to look for a suitable method in other
215 ancestors of C<$self> -- whereas C<$self-E<gt>SUPER::m()> cannot.
217 A typical use would be in the destructors of a class hierarchy,
218 as illustrated in the synopsis above. Each class in the hierarchy
219 has a DESTROY method that performs some class-specific action
220 and then redispatches the call up the hierarchy. As a result,
221 when an object of class D is destroyed, the destructors of I<all>
222 its parent classes are called (in depth-first, left-to-right order).
224 Another typical use of redispatch would be in C<AUTOLOAD>'ed methods.
225 If such a method determined that it was not able to handle a
226 particular call, it might choose to redispatch that call, in the
227 hope that some other C<AUTOLOAD> (above it, or to its left) might
228 do better.
230 By default, if a redispatch attempt fails to find another method
231 elsewhere in the objects class hierarchy, it quietly gives up and does
232 nothing (but see L<"Enforcing redispatch">). This gracious acquiesence
233 is also unlike the (generally annoying) behaviour of C<SUPER>, which
234 throws an exception if it cannot redispatch.
236 Note that it is a fatal error for any method (including C<AUTOLOAD>)
237 to attempt to redispatch any method that does not have the
238 same name. For example:
240 sub D::oops { print "oops!\n"; $_[0]->NEXT::other_method() }
243 =head2 Enforcing redispatch
245 It is possible to make C<NEXT> redispatch more demandingly (i.e. like
246 C<SUPER> does), so that the redispatch throws an exception if it cannot
247 find a "next" method to call.
249 To do this, simple invoke the redispatch as:
251 $self->NEXT::ACTUAL::method();
253 rather than:
255 $self->NEXT::method();
257 The C<ACTUAL> tells C<NEXT> that there must actually be a next method to call,
258 or it should throw an exception.
260 C<NEXT::ACTUAL> is most commonly used in C<AUTOLOAD> methods, as a means to
261 decline an C<AUTOLOAD> request, but preserve the normal exception-on-failure
262 semantics:
264 sub AUTOLOAD {
265 if ($AUTOLOAD =~ /foo|bar/) {
266 # handle here
268 else { # try elsewhere
269 shift()->NEXT::ACTUAL::AUTOLOAD(@_);
273 By using C<NEXT::ACTUAL>, if there is no other C<AUTOLOAD> to handle the
274 method call, an exception will be thrown (as usually happens in the absence of
275 a suitable C<AUTOLOAD>).
278 =head2 Avoiding repetitions
280 If C<NEXT> redispatching is used in the methods of a "diamond" class hierarchy:
282 # A B
283 # / \ /
284 # C D
285 # \ /
288 use NEXT;
290 package A;
291 sub foo { print "called A::foo\n"; shift->NEXT::foo() }
293 package B;
294 sub foo { print "called B::foo\n"; shift->NEXT::foo() }
296 package C; @ISA = qw( A );
297 sub foo { print "called C::foo\n"; shift->NEXT::foo() }
299 package D; @ISA = qw(A B);
300 sub foo { print "called D::foo\n"; shift->NEXT::foo() }
302 package E; @ISA = qw(C D);
303 sub foo { print "called E::foo\n"; shift->NEXT::foo() }
305 E->foo();
307 then derived classes may (re-)inherit base-class methods through two or
308 more distinct paths (e.g. in the way C<E> inherits C<A::foo> twice --
309 through C<C> and C<D>). In such cases, a sequence of C<NEXT> redispatches
310 will invoke the multiply inherited method as many times as it is
311 inherited. For example, the above code prints:
313 called E::foo
314 called C::foo
315 called A::foo
316 called D::foo
317 called A::foo
318 called B::foo
320 (i.e. C<A::foo> is called twice).
322 In some cases this I<may> be the desired effect within a diamond hierarchy,
323 but in others (e.g. for destructors) it may be more appropriate to
324 call each method only once during a sequence of redispatches.
326 To cover such cases, you can redispatch methods via:
328 $self->NEXT::DISTINCT::method();
330 rather than:
332 $self->NEXT::method();
334 This causes the redispatcher to only visit each distinct C<method> method
335 once. That is, to skip any classes in the hierarchy that it has
336 already visited during redispatch. So, for example, if the
337 previous example were rewritten:
339 package A;
340 sub foo { print "called A::foo\n"; shift->NEXT::DISTINCT::foo() }
342 package B;
343 sub foo { print "called B::foo\n"; shift->NEXT::DISTINCT::foo() }
345 package C; @ISA = qw( A );
346 sub foo { print "called C::foo\n"; shift->NEXT::DISTINCT::foo() }
348 package D; @ISA = qw(A B);
349 sub foo { print "called D::foo\n"; shift->NEXT::DISTINCT::foo() }
351 package E; @ISA = qw(C D);
352 sub foo { print "called E::foo\n"; shift->NEXT::DISTINCT::foo() }
354 E->foo();
356 then it would print:
358 called E::foo
359 called C::foo
360 called A::foo
361 called D::foo
362 called B::foo
364 and omit the second call to C<A::foo> (since it would not be distinct
365 from the first call to C<A::foo>).
367 Note that you can also use:
369 $self->NEXT::DISTINCT::ACTUAL::method();
373 $self->NEXT::ACTUAL::DISTINCT::method();
375 to get both unique invocation I<and> exception-on-failure.
377 Note that, for historical compatibility, you can also use
378 C<NEXT::UNSEEN> instead of C<NEXT::DISTINCT>.
381 =head2 Invoking all versions of a method with a single call
383 Yet another pseudo-class that NEXT.pm provides is C<EVERY>.
384 Its behaviour is considerably simpler than that of the C<NEXT> family.
385 A call to:
387 $obj->EVERY::foo();
389 calls I<every> method named C<foo> that the object in C<$obj> has inherited.
390 That is:
392 use NEXT;
394 package A; @ISA = qw(B D X);
395 sub foo { print "A::foo " }
397 package B; @ISA = qw(D X);
398 sub foo { print "B::foo " }
400 package X; @ISA = qw(D);
401 sub foo { print "X::foo " }
403 package D;
404 sub foo { print "D::foo " }
406 package main;
408 my $obj = bless {}, 'A';
409 $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
411 Prefixing a method call with C<EVERY::> causes every method in the
412 object's hierarchy with that name to be invoked. As the above example
413 illustrates, they are not called in Perl's usual "left-most-depth-first"
414 order. Instead, they are called "breadth-first-dependency-wise".
416 That means that the inheritance tree of the object is traversed breadth-first
417 and the resulting order of classes is used as the sequence in which methods
418 are called. However, that sequence is modified by imposing a rule that the
419 appropritae method of a derived class must be called before the same method of
420 any ancestral class. That's why, in the above example, C<X::foo> is called
421 before C<D::foo>, even though C<D> comes before C<X> in C<@B::ISA>.
423 In general, there's no need to worry about the order of calls. They will be
424 left-to-right, breadth-first, most-derived-first. This works perfectly for
425 most inherited methods (including destructors), but is inappropriate for
426 some kinds of methods (such as constructors, cloners, debuggers, and
427 initializers) where it's more appropriate that the least-derived methods be
428 called first (as more-derived methods may rely on the behaviour of their
429 "ancestors"). In that case, instead of using the C<EVERY> pseudo-class:
431 $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
433 you can use the C<EVERY::LAST> pseudo-class:
435 $obj->EVERY::LAST::foo(); # prints" D::foo X::foo B::foo A::foo
437 which reverses the order of method call.
439 Whichever version is used, the actual methods are called in the same
440 context (list, scalar, or void) as the original call via C<EVERY>, and return:
442 =over
444 =item *
446 A hash of array references in list context. Each entry of the hash has the
447 fully qualified method name as its key and a reference to an array containing
448 the method's list-context return values as its value.
450 =item *
452 A reference to a hash of scalar values in scalar context. Each entry of the hash has the
453 fully qualified method name as its key and the method's scalar-context return values as its value.
455 =item *
457 Nothing in void context (obviously).
459 =back
461 =head2 Using C<EVERY> methods
463 The typical way to use an C<EVERY> call is to wrap it in another base
464 method, that all classes inherit. For example, to ensure that every
465 destructor an object inherits is actually called (as opposed to just the
466 left-most-depth-first-est one):
468 package Base;
469 sub DESTROY { $_[0]->EVERY::Destroy }
471 package Derived1;
472 use base 'Base';
473 sub Destroy {...}
475 package Derived2;
476 use base 'Base', 'Derived1';
477 sub Destroy {...}
479 et cetera. Every derived class than needs its own clean-up
480 behaviour simply adds its own C<Destroy> method (I<not> a C<DESTROY> method),
481 which the call to C<EVERY::LAST::Destroy> in the inherited destructor
482 then correctly picks up.
484 Likewise, to create a class hierarchy in which every initializer inherited by
485 a new object is invoked:
487 package Base;
488 sub new {
489 my ($class, %args) = @_;
490 my $obj = bless {}, $class;
491 $obj->EVERY::LAST::Init(\%args);
494 package Derived1;
495 use base 'Base';
496 sub Init {
497 my ($argsref) = @_;
501 package Derived2;
502 use base 'Base', 'Derived1';
503 sub Init {
504 my ($argsref) = @_;
508 et cetera. Every derived class than needs some additional initialization
509 behaviour simply adds its own C<Init> method (I<not> a C<new> method),
510 which the call to C<EVERY::LAST::Init> in the inherited constructor
511 then correctly picks up.
514 =head1 AUTHOR
516 Damian Conway (damian@conway.org)
518 =head1 BUGS AND IRRITATIONS
520 Because it's a module, not an integral part of the interpreter, NEXT.pm
521 has to guess where the surrounding call was found in the method
522 look-up sequence. In the presence of diamond inheritance patterns
523 it occasionally guesses wrong.
525 It's also too slow (despite caching).
527 Comment, suggestions, and patches welcome.
529 =head1 COPYRIGHT
531 Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
532 This module is free software. It may be used, redistributed
533 and/or modified under the same terms as Perl itself.