This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_hv_placeholders_get() actually takes a const HV *hv.
[perl5.git] / lib / NEXT.pm
CommitLineData
e4783b1c 1package NEXT;
ccea220c 2$VERSION = '0.61';
e4783b1c
JH
3use Carp;
4use strict;
5dd54fb4 5use overload ();
e4783b1c 6
52138ef3 7sub NEXT::ELSEWHERE::ancestors
e4783b1c 8{
13021a80 9 my @inlist = shift;
e4783b1c 10 my @outlist = ();
13021a80
JH
11 while (my $next = shift @inlist) {
12 push @outlist, $next;
e4783b1c
JH
13 no strict 'refs';
14 unshift @inlist, @{"$outlist[-1]::ISA"};
15 }
16 return @outlist;
17}
18
bf5734d4
JH
19sub NEXT::ELSEWHERE::ordered_ancestors
20{
21 my @inlist = shift;
22 my @outlist = ();
23 while (my $next = shift @inlist) {
24 push @outlist, $next;
25 no strict 'refs';
26 push @inlist, @{"$outlist[-1]::ISA"};
27 }
28 return sort { $a->isa($b) ? -1
29 : $b->isa($a) ? +1
30 : 0 } @outlist;
31}
32
597fc7a0 33sub NEXT::ELSEWHERE::buildAUTOLOAD
e4783b1c 34{
597fc7a0
RGS
35 my $autoload_name = caller() . '::AUTOLOAD';
36
37 no strict 'refs';
38 *{$autoload_name} = sub {
39 my ($self) = @_;
40 my $depth = 1;
41 until (((caller($depth))[3]||q{}) !~ /^\(eval\)$/) { $depth++ }
42 my $caller = (caller($depth))[3];
43 my $wanted = $NEXT::AUTOLOAD || $autoload_name;
44 undef $NEXT::AUTOLOAD;
45 my ($caller_class, $caller_method) = $caller =~ m{(.*)::(.*)}g;
46 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
47 croak "Can't call $wanted from $caller"
48 unless $caller_method eq $wanted_method;
49
50 my $key = ref $self && overload::Overloaded($self)
51 ? overload::StrVal($self) : $self;
52
53 local ($NEXT::NEXT{$key,$wanted_method}, $NEXT::SEEN) =
54 ($NEXT::NEXT{$key,$wanted_method}, $NEXT::SEEN);
55
56 unless ($NEXT::NEXT{$key,$wanted_method}) {
57 my @forebears =
58 NEXT::ELSEWHERE::ancestors ref $self || $self,
59 $wanted_class;
60 while (@forebears) {
61 last if shift @forebears eq $caller_class
62 }
63 no strict 'refs';
64 @{$NEXT::NEXT{$key,$wanted_method}} =
65 map { *{"${_}::$caller_method"}{CODE}||() } @forebears
66 unless $wanted_method eq 'AUTOLOAD';
67 @{$NEXT::NEXT{$key,$wanted_method}} =
68 map { (*{"${_}::AUTOLOAD"}{CODE}) ? "${_}::AUTOLOAD" : ()} @forebears
69 unless @{$NEXT::NEXT{$key,$wanted_method}||[]};
70 $NEXT::SEEN->{$key,*{$caller}{CODE}}++;
71 }
72 my $call_method = shift @{$NEXT::NEXT{$key,$wanted_method}};
73 while ($wanted_class =~ /^NEXT\b.*\b(UNSEEN|DISTINCT)\b/
74 && defined $call_method
75 && $NEXT::SEEN->{$key,$call_method}++) {
76 $call_method = shift @{$NEXT::NEXT{$key,$wanted_method}};
77 }
78 unless (defined $call_method) {
79 return unless $wanted_class =~ /^NEXT:.*:ACTUAL/;
80 (local $Carp::CarpLevel)++;
81 croak qq(Can't locate object method "$wanted_method" ),
82 qq(via package "$caller_class");
83 };
84 return $self->$call_method(@_[1..$#_]) if ref $call_method eq 'CODE';
85 no strict 'refs';
86 ($wanted_method=${$caller_class."::AUTOLOAD"}) =~ s/.*:://
87 if $wanted_method eq 'AUTOLOAD';
88 $$call_method = $caller_class."::NEXT::".$wanted_method;
89 return $call_method->(@_);
90 };
e4783b1c
JH
91}
92
13021a80 93no strict 'vars';
597fc7a0
RGS
94package NEXT; NEXT::ELSEWHERE::buildAUTOLOAD();
95package NEXT::UNSEEN; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
96package NEXT::DISTINCT; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
97package NEXT::ACTUAL; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
98package NEXT::ACTUAL::UNSEEN; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
99package NEXT::ACTUAL::DISTINCT; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
100package NEXT::UNSEEN::ACTUAL; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
101package NEXT::DISTINCT::ACTUAL; @ISA = 'NEXT'; NEXT::ELSEWHERE::buildAUTOLOAD();
102
103package EVERY;
104
105sub EVERY::ELSEWHERE::buildAUTOLOAD {
106 my $autoload_name = caller() . '::AUTOLOAD';
107
108 no strict 'refs';
109 *{$autoload_name} = sub {
110 my ($self) = @_;
111 my $depth = 1;
112 until (((caller($depth))[3]||q{}) !~ /^\(eval\)$/) { $depth++ }
113 my $caller = (caller($depth))[3];
114 my $wanted = $EVERY::AUTOLOAD || $autoload_name;
115 undef $EVERY::AUTOLOAD;
116 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
117
118 my $key = ref($self) && overload::Overloaded($self)
119 ? overload::StrVal($self) : $self;
120
121 local $NEXT::ALREADY_IN_EVERY{$key,$wanted_method} =
122 $NEXT::ALREADY_IN_EVERY{$key,$wanted_method};
123
124 return if $NEXT::ALREADY_IN_EVERY{$key,$wanted_method}++;
125
126 my @forebears = NEXT::ELSEWHERE::ordered_ancestors ref $self || $self,
127 $wanted_class;
128 @forebears = reverse @forebears if $wanted_class =~ /\bLAST\b/;
129 no strict 'refs';
130 my %seen;
131 my @every = map { my $sub = "${_}::$wanted_method";
132 !*{$sub}{CODE} || $seen{$sub}++ ? () : $sub
133 } @forebears
134 unless $wanted_method eq 'AUTOLOAD';
135
136 my $want = wantarray;
137 if (@every) {
138 if ($want) {
139 return map {($_, [$self->$_(@_[1..$#_])])} @every;
140 }
141 elsif (defined $want) {
142 return { map {($_, scalar($self->$_(@_[1..$#_])))}
143 @every
144 };
145 }
146 else {
147 $self->$_(@_[1..$#_]) for @every;
148 return;
149 }
150 }
151
152 @every = map { my $sub = "${_}::AUTOLOAD";
153 !*{$sub}{CODE} || $seen{$sub}++ ? () : "${_}::AUTOLOAD"
154 } @forebears;
155 if ($want) {
156 return map { $$_ = ref($self)."::EVERY::".$wanted_method;
157 ($_, [$self->$_(@_[1..$#_])]);
158 } @every;
159 }
160 elsif (defined $want) {
161 return { map { $$_ = ref($self)."::EVERY::".$wanted_method;
162 ($_, scalar($self->$_(@_[1..$#_])))
163 } @every
164 };
165 }
166 else {
167 for (@every) {
168 $$_ = ref($self)."::EVERY::".$wanted_method;
169 $self->$_(@_[1..$#_]);
170 }
171 return;
172 }
173 };
bf5734d4
JH
174}
175
597fc7a0
RGS
176package EVERY::LAST; @ISA = 'EVERY'; EVERY::ELSEWHERE::buildAUTOLOAD();
177package EVERY; @ISA = 'NEXT'; EVERY::ELSEWHERE::buildAUTOLOAD();
13021a80 178
e4783b1c
JH
1791;
180
181__END__
182
183=head1 NAME
184
bf5734d4 185NEXT.pm - Provide a pseudo-class NEXT (et al) that allows method redispatch
e4783b1c
JH
186
187
188=head1 SYNOPSIS
189
13021a80 190 use NEXT;
e4783b1c 191
13021a80
JH
192 package A;
193 sub A::method { print "$_[0]: A method\n"; $_[0]->NEXT::method() }
194 sub A::DESTROY { print "$_[0]: A dtor\n"; $_[0]->NEXT::DESTROY() }
e4783b1c 195
13021a80
JH
196 package B;
197 use base qw( A );
198 sub B::AUTOLOAD { print "$_[0]: B AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
199 sub B::DESTROY { print "$_[0]: B dtor\n"; $_[0]->NEXT::DESTROY() }
e4783b1c 200
13021a80
JH
201 package C;
202 sub C::method { print "$_[0]: C method\n"; $_[0]->NEXT::method() }
203 sub C::AUTOLOAD { print "$_[0]: C AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
204 sub C::DESTROY { print "$_[0]: C dtor\n"; $_[0]->NEXT::DESTROY() }
e4783b1c 205
13021a80
JH
206 package D;
207 use base qw( B C );
208 sub D::method { print "$_[0]: D method\n"; $_[0]->NEXT::method() }
209 sub D::AUTOLOAD { print "$_[0]: D AUTOLOAD\n"; $_[0]->NEXT::AUTOLOAD() }
210 sub D::DESTROY { print "$_[0]: D dtor\n"; $_[0]->NEXT::DESTROY() }
e4783b1c 211
13021a80 212 package main;
e4783b1c 213
13021a80 214 my $obj = bless {}, "D";
e4783b1c 215
13021a80
JH
216 $obj->method(); # Calls D::method, A::method, C::method
217 $obj->missing_method(); # Calls D::AUTOLOAD, B::AUTOLOAD, C::AUTOLOAD
e4783b1c 218
13021a80 219 # Clean-up calls D::DESTROY, B::DESTROY, A::DESTROY, C::DESTROY
e4783b1c
JH
220
221
bf5734d4 222
e4783b1c
JH
223=head1 DESCRIPTION
224
225NEXT.pm adds a pseudoclass named C<NEXT> to any program
e23eab12 226that uses it. If a method C<m> calls C<$self-E<gt>NEXT::m()>, the call to
e4783b1c
JH
227C<m> is redispatched as if the calling method had not originally been found.
228
e23eab12 229In other words, a call to C<$self-E<gt>NEXT::m()> resumes the depth-first,
55a1c97c
JH
230left-to-right search of C<$self>'s class hierarchy that resulted in the
231original call to C<m>.
232
e23eab12 233Note that this is not the same thing as C<$self-E<gt>SUPER::m()>, which
55a1c97c 234begins a new dispatch that is restricted to searching the ancestors
e23eab12 235of the current class. C<$self-E<gt>NEXT::m()> can backtrack
55a1c97c 236past the current class -- to look for a suitable method in other
e23eab12 237ancestors of C<$self> -- whereas C<$self-E<gt>SUPER::m()> cannot.
e4783b1c
JH
238
239A typical use would be in the destructors of a class hierarchy,
240as illustrated in the synopsis above. Each class in the hierarchy
241has a DESTROY method that performs some class-specific action
242and then redispatches the call up the hierarchy. As a result,
243when an object of class D is destroyed, the destructors of I<all>
244its parent classes are called (in depth-first, left-to-right order).
245
246Another typical use of redispatch would be in C<AUTOLOAD>'ed methods.
247If such a method determined that it was not able to handle a
248particular call, it might choose to redispatch that call, in the
249hope that some other C<AUTOLOAD> (above it, or to its left) might
250do better.
251
13021a80
JH
252By default, if a redispatch attempt fails to find another method
253elsewhere in the objects class hierarchy, it quietly gives up and does
3c4b39be 254nothing (but see L<"Enforcing redispatch">). This gracious acquiescence
13021a80
JH
255is also unlike the (generally annoying) behaviour of C<SUPER>, which
256throws an exception if it cannot redispatch.
257
e4783b1c 258Note that it is a fatal error for any method (including C<AUTOLOAD>)
13021a80
JH
259to attempt to redispatch any method that does not have the
260same name. For example:
261
262 sub D::oops { print "oops!\n"; $_[0]->NEXT::other_method() }
263
264
265=head2 Enforcing redispatch
266
267It is possible to make C<NEXT> redispatch more demandingly (i.e. like
268C<SUPER> does), so that the redispatch throws an exception if it cannot
269find a "next" method to call.
270
271To do this, simple invoke the redispatch as:
272
273 $self->NEXT::ACTUAL::method();
274
275rather than:
276
277 $self->NEXT::method();
278
279The C<ACTUAL> tells C<NEXT> that there must actually be a next method to call,
280or it should throw an exception.
281
282C<NEXT::ACTUAL> is most commonly used in C<AUTOLOAD> methods, as a means to
283decline an C<AUTOLOAD> request, but preserve the normal exception-on-failure
284semantics:
285
286 sub AUTOLOAD {
287 if ($AUTOLOAD =~ /foo|bar/) {
288 # handle here
289 }
290 else { # try elsewhere
291 shift()->NEXT::ACTUAL::AUTOLOAD(@_);
292 }
293 }
294
295By using C<NEXT::ACTUAL>, if there is no other C<AUTOLOAD> to handle the
296method call, an exception will be thrown (as usually happens in the absence of
297a suitable C<AUTOLOAD>).
298
299
300=head2 Avoiding repetitions
301
302If C<NEXT> redispatching is used in the methods of a "diamond" class hierarchy:
303
304 # A B
305 # / \ /
306 # C D
307 # \ /
308 # E
309
310 use NEXT;
311
312 package A;
313 sub foo { print "called A::foo\n"; shift->NEXT::foo() }
314
315 package B;
316 sub foo { print "called B::foo\n"; shift->NEXT::foo() }
317
318 package C; @ISA = qw( A );
319 sub foo { print "called C::foo\n"; shift->NEXT::foo() }
320
321 package D; @ISA = qw(A B);
322 sub foo { print "called D::foo\n"; shift->NEXT::foo() }
323
324 package E; @ISA = qw(C D);
325 sub foo { print "called E::foo\n"; shift->NEXT::foo() }
326
327 E->foo();
328
329then derived classes may (re-)inherit base-class methods through two or
330more distinct paths (e.g. in the way C<E> inherits C<A::foo> twice --
331through C<C> and C<D>). In such cases, a sequence of C<NEXT> redispatches
332will invoke the multiply inherited method as many times as it is
333inherited. For example, the above code prints:
334
335 called E::foo
336 called C::foo
337 called A::foo
338 called D::foo
339 called A::foo
340 called B::foo
341
342(i.e. C<A::foo> is called twice).
343
344In some cases this I<may> be the desired effect within a diamond hierarchy,
345but in others (e.g. for destructors) it may be more appropriate to
346call each method only once during a sequence of redispatches.
347
348To cover such cases, you can redispatch methods via:
349
52138ef3 350 $self->NEXT::DISTINCT::method();
13021a80
JH
351
352rather than:
353
354 $self->NEXT::method();
355
52138ef3
JH
356This causes the redispatcher to only visit each distinct C<method> method
357once. That is, to skip any classes in the hierarchy that it has
358already visited during redispatch. So, for example, if the
13021a80
JH
359previous example were rewritten:
360
361 package A;
52138ef3 362 sub foo { print "called A::foo\n"; shift->NEXT::DISTINCT::foo() }
13021a80
JH
363
364 package B;
52138ef3 365 sub foo { print "called B::foo\n"; shift->NEXT::DISTINCT::foo() }
13021a80
JH
366
367 package C; @ISA = qw( A );
52138ef3 368 sub foo { print "called C::foo\n"; shift->NEXT::DISTINCT::foo() }
13021a80
JH
369
370 package D; @ISA = qw(A B);
52138ef3 371 sub foo { print "called D::foo\n"; shift->NEXT::DISTINCT::foo() }
13021a80
JH
372
373 package E; @ISA = qw(C D);
52138ef3 374 sub foo { print "called E::foo\n"; shift->NEXT::DISTINCT::foo() }
13021a80
JH
375
376 E->foo();
377
378then it would print:
379
380 called E::foo
381 called C::foo
382 called A::foo
383 called D::foo
384 called B::foo
385
52138ef3
JH
386and omit the second call to C<A::foo> (since it would not be distinct
387from the first call to C<A::foo>).
13021a80
JH
388
389Note that you can also use:
390
52138ef3 391 $self->NEXT::DISTINCT::ACTUAL::method();
13021a80
JH
392
393or:
394
52138ef3 395 $self->NEXT::ACTUAL::DISTINCT::method();
e4783b1c 396
13021a80 397to get both unique invocation I<and> exception-on-failure.
e4783b1c 398
52138ef3
JH
399Note that, for historical compatibility, you can also use
400C<NEXT::UNSEEN> instead of C<NEXT::DISTINCT>.
e4783b1c 401
bf5734d4
JH
402
403=head2 Invoking all versions of a method with a single call
404
405Yet another pseudo-class that NEXT.pm provides is C<EVERY>.
406Its behaviour is considerably simpler than that of the C<NEXT> family.
407A call to:
408
409 $obj->EVERY::foo();
410
411calls I<every> method named C<foo> that the object in C<$obj> has inherited.
412That is:
413
414 use NEXT;
415
416 package A; @ISA = qw(B D X);
417 sub foo { print "A::foo " }
418
419 package B; @ISA = qw(D X);
420 sub foo { print "B::foo " }
421
422 package X; @ISA = qw(D);
423 sub foo { print "X::foo " }
424
425 package D;
426 sub foo { print "D::foo " }
427
428 package main;
429
430 my $obj = bless {}, 'A';
431 $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
432
433Prefixing a method call with C<EVERY::> causes every method in the
434object's hierarchy with that name to be invoked. As the above example
435illustrates, they are not called in Perl's usual "left-most-depth-first"
436order. Instead, they are called "breadth-first-dependency-wise".
437
438That means that the inheritance tree of the object is traversed breadth-first
439and the resulting order of classes is used as the sequence in which methods
440are called. However, that sequence is modified by imposing a rule that the
3c4b39be 441appropriate method of a derived class must be called before the same method of
bf5734d4
JH
442any ancestral class. That's why, in the above example, C<X::foo> is called
443before C<D::foo>, even though C<D> comes before C<X> in C<@B::ISA>.
444
445In general, there's no need to worry about the order of calls. They will be
446left-to-right, breadth-first, most-derived-first. This works perfectly for
447most inherited methods (including destructors), but is inappropriate for
448some kinds of methods (such as constructors, cloners, debuggers, and
449initializers) where it's more appropriate that the least-derived methods be
450called first (as more-derived methods may rely on the behaviour of their
451"ancestors"). In that case, instead of using the C<EVERY> pseudo-class:
452
453 $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
454
455you can use the C<EVERY::LAST> pseudo-class:
456
457 $obj->EVERY::LAST::foo(); # prints" D::foo X::foo B::foo A::foo
458
459which reverses the order of method call.
460
461Whichever version is used, the actual methods are called in the same
462context (list, scalar, or void) as the original call via C<EVERY>, and return:
463
464=over
465
466=item *
467
468A hash of array references in list context. Each entry of the hash has the
469fully qualified method name as its key and a reference to an array containing
470the method's list-context return values as its value.
471
472=item *
473
474A reference to a hash of scalar values in scalar context. Each entry of the hash has the
475fully qualified method name as its key and the method's scalar-context return values as its value.
476
477=item *
478
479Nothing in void context (obviously).
480
481=back
482
483=head2 Using C<EVERY> methods
484
485The typical way to use an C<EVERY> call is to wrap it in another base
486method, that all classes inherit. For example, to ensure that every
487destructor an object inherits is actually called (as opposed to just the
488left-most-depth-first-est one):
489
490 package Base;
491 sub DESTROY { $_[0]->EVERY::Destroy }
492
493 package Derived1;
494 use base 'Base';
495 sub Destroy {...}
496
497 package Derived2;
498 use base 'Base', 'Derived1';
499 sub Destroy {...}
500
501et cetera. Every derived class than needs its own clean-up
502behaviour simply adds its own C<Destroy> method (I<not> a C<DESTROY> method),
503which the call to C<EVERY::LAST::Destroy> in the inherited destructor
504then correctly picks up.
505
506Likewise, to create a class hierarchy in which every initializer inherited by
507a new object is invoked:
508
509 package Base;
510 sub new {
511 my ($class, %args) = @_;
512 my $obj = bless {}, $class;
513 $obj->EVERY::LAST::Init(\%args);
514 }
515
516 package Derived1;
517 use base 'Base';
518 sub Init {
519 my ($argsref) = @_;
520 ...
521 }
522
523 package Derived2;
524 use base 'Base', 'Derived1';
525 sub Init {
526 my ($argsref) = @_;
527 ...
528 }
529
530et cetera. Every derived class than needs some additional initialization
531behaviour simply adds its own C<Init> method (I<not> a C<new> method),
532which the call to C<EVERY::LAST::Init> in the inherited constructor
533then correctly picks up.
534
535
e4783b1c
JH
536=head1 AUTHOR
537
538Damian Conway (damian@conway.org)
539
540=head1 BUGS AND IRRITATIONS
541
542Because it's a module, not an integral part of the interpreter, NEXT.pm
543has to guess where the surrounding call was found in the method
544look-up sequence. In the presence of diamond inheritance patterns
545it occasionally guesses wrong.
546
547It's also too slow (despite caching).
548
549Comment, suggestions, and patches welcome.
550
551=head1 COPYRIGHT
552
55a1c97c 553 Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
e4783b1c 554 This module is free software. It may be used, redistributed
55a1c97c 555 and/or modified under the same terms as Perl itself.