Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man3 / NEXT.3
.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "NEXT 3"
.TH NEXT 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
.SH "NAME"
NEXT.pm \- Provide a pseudo\-class NEXT (et al) that allows method redispatch
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use NEXT;
.Ve
.PP
.Vb 3
\& package A;
\& sub A::method { print "$_[0]: A method\en"; $_[0]->NEXT::method() }
\& sub A::DESTROY { print "$_[0]: A dtor\en"; $_[0]->NEXT::DESTROY() }
.Ve
.PP
.Vb 4
\& package B;
\& use base qw( A );
\& sub B::AUTOLOAD { print "$_[0]: B AUTOLOAD\en"; $_[0]->NEXT::AUTOLOAD() }
\& sub B::DESTROY { print "$_[0]: B dtor\en"; $_[0]->NEXT::DESTROY() }
.Ve
.PP
.Vb 4
\& package C;
\& sub C::method { print "$_[0]: C method\en"; $_[0]->NEXT::method() }
\& sub C::AUTOLOAD { print "$_[0]: C AUTOLOAD\en"; $_[0]->NEXT::AUTOLOAD() }
\& sub C::DESTROY { print "$_[0]: C dtor\en"; $_[0]->NEXT::DESTROY() }
.Ve
.PP
.Vb 5
\& package D;
\& use base qw( B C );
\& sub D::method { print "$_[0]: D method\en"; $_[0]->NEXT::method() }
\& sub D::AUTOLOAD { print "$_[0]: D AUTOLOAD\en"; $_[0]->NEXT::AUTOLOAD() }
\& sub D::DESTROY { print "$_[0]: D dtor\en"; $_[0]->NEXT::DESTROY() }
.Ve
.PP
.Vb 1
\& package main;
.Ve
.PP
.Vb 1
\& my $obj = bless {}, "D";
.Ve
.PP
.Vb 2
\& $obj->method(); # Calls D::method, A::method, C::method
\& $obj->missing_method(); # Calls D::AUTOLOAD, B::AUTOLOAD, C::AUTOLOAD
.Ve
.PP
.Vb 1
\& # Clean-up calls D::DESTROY, B::DESTROY, A::DESTROY, C::DESTROY
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\s-1NEXT\s0.pm adds a pseudoclass named \f(CW\*(C`NEXT\*(C'\fR to any program
that uses it. If a method \f(CW\*(C`m\*(C'\fR calls \f(CW\*(C`$self\->NEXT::m()\*(C'\fR, the call to
\&\f(CW\*(C`m\*(C'\fR is redispatched as if the calling method had not originally been found.
.PP
In other words, a call to \f(CW\*(C`$self\->NEXT::m()\*(C'\fR resumes the depth\-first,
left-to-right search of \f(CW$self\fR's class hierarchy that resulted in the
original call to \f(CW\*(C`m\*(C'\fR.
.PP
Note that this is not the same thing as \f(CW\*(C`$self\->SUPER::m()\*(C'\fR, which
begins a new dispatch that is restricted to searching the ancestors
of the current class. \f(CW\*(C`$self\->NEXT::m()\*(C'\fR can backtrack
past the current class \*(-- to look for a suitable method in other
ancestors of \f(CW$self\fR \*(-- whereas \f(CW\*(C`$self\->SUPER::m()\*(C'\fR cannot.
.PP
A typical use would be in the destructors of a class hierarchy,
as illustrated in the synopsis above. Each class in the hierarchy
has a \s-1DESTROY\s0 method that performs some class-specific action
and then redispatches the call up the hierarchy. As a result,
when an object of class D is destroyed, the destructors of \fIall\fR
its parent classes are called (in depth\-first, left-to-right order).
.PP
Another typical use of redispatch would be in \f(CW\*(C`AUTOLOAD\*(C'\fR'ed methods.
If such a method determined that it was not able to handle a
particular call, it might choose to redispatch that call, in the
hope that some other \f(CW\*(C`AUTOLOAD\*(C'\fR (above it, or to its left) might
do better.
.PP
By default, if a redispatch attempt fails to find another method
elsewhere in the objects class hierarchy, it quietly gives up and does
nothing (but see \*(L"Enforcing redispatch\*(R"). This gracious acquiesence
is also unlike the (generally annoying) behaviour of \f(CW\*(C`SUPER\*(C'\fR, which
throws an exception if it cannot redispatch.
.PP
Note that it is a fatal error for any method (including \f(CW\*(C`AUTOLOAD\*(C'\fR)
to attempt to redispatch any method that does not have the
same name. For example:
.PP
.Vb 1
\& sub D::oops { print "oops!\en"; $_[0]->NEXT::other_method() }
.Ve
.Sh "Enforcing redispatch"
.IX Subsection "Enforcing redispatch"
It is possible to make \f(CW\*(C`NEXT\*(C'\fR redispatch more demandingly (i.e. like
\&\f(CW\*(C`SUPER\*(C'\fR does), so that the redispatch throws an exception if it cannot
find a \*(L"next\*(R" method to call.
.PP
To do this, simple invoke the redispatch as:
.PP
.Vb 1
\& $self->NEXT::ACTUAL::method();
.Ve
.PP
rather than:
.PP
.Vb 1
\& $self->NEXT::method();
.Ve
.PP
The \f(CW\*(C`ACTUAL\*(C'\fR tells \f(CW\*(C`NEXT\*(C'\fR that there must actually be a next method to call,
or it should throw an exception.
.PP
\&\f(CW\*(C`NEXT::ACTUAL\*(C'\fR is most commonly used in \f(CW\*(C`AUTOLOAD\*(C'\fR methods, as a means to
decline an \f(CW\*(C`AUTOLOAD\*(C'\fR request, but preserve the normal exception-on-failure
semantics:
.PP
.Vb 8
\& sub AUTOLOAD {
\& if ($AUTOLOAD =~ /foo|bar/) {
\& # handle here
\& }
\& else { # try elsewhere
\& shift()->NEXT::ACTUAL::AUTOLOAD(@_);
\& }
\& }
.Ve
.PP
By using \f(CW\*(C`NEXT::ACTUAL\*(C'\fR, if there is no other \f(CW\*(C`AUTOLOAD\*(C'\fR to handle the
method call, an exception will be thrown (as usually happens in the absence of
a suitable \f(CW\*(C`AUTOLOAD\*(C'\fR).
.Sh "Avoiding repetitions"
.IX Subsection "Avoiding repetitions"
If \f(CW\*(C`NEXT\*(C'\fR redispatching is used in the methods of a \*(L"diamond\*(R" class hierarchy:
.PP
.Vb 5
\& # A B
\& # / \e /
\& # C D
\& # \e /
\& # E
.Ve
.PP
.Vb 1
\& use NEXT;
.Ve
.PP
.Vb 2
\& package A;
\& sub foo { print "called A::foo\en"; shift->NEXT::foo() }
.Ve
.PP
.Vb 2
\& package B;
\& sub foo { print "called B::foo\en"; shift->NEXT::foo() }
.Ve
.PP
.Vb 2
\& package C; @ISA = qw( A );
\& sub foo { print "called C::foo\en"; shift->NEXT::foo() }
.Ve
.PP
.Vb 2
\& package D; @ISA = qw(A B);
\& sub foo { print "called D::foo\en"; shift->NEXT::foo() }
.Ve
.PP
.Vb 2
\& package E; @ISA = qw(C D);
\& sub foo { print "called E::foo\en"; shift->NEXT::foo() }
.Ve
.PP
.Vb 1
\& E->foo();
.Ve
.PP
then derived classes may (re\-)inherit base-class methods through two or
more distinct paths (e.g. in the way \f(CW\*(C`E\*(C'\fR inherits \f(CW\*(C`A::foo\*(C'\fR twice \*(--
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
will invoke the multiply inherited method as many times as it is
inherited. For example, the above code prints:
.PP
.Vb 6
\& called E::foo
\& called C::foo
\& called A::foo
\& called D::foo
\& called A::foo
\& called B::foo
.Ve
.PP
(i.e. \f(CW\*(C`A::foo\*(C'\fR is called twice).
.PP
In some cases this \fImay\fR be the desired effect within a diamond hierarchy,
but in others (e.g. for destructors) it may be more appropriate to
call each method only once during a sequence of redispatches.
.PP
To cover such cases, you can redispatch methods via:
.PP
.Vb 1
\& $self->NEXT::DISTINCT::method();
.Ve
.PP
rather than:
.PP
.Vb 1
\& $self->NEXT::method();
.Ve
.PP
This causes the redispatcher to only visit each distinct \f(CW\*(C`method\*(C'\fR method
once. That is, to skip any classes in the hierarchy that it has
already visited during redispatch. So, for example, if the
previous example were rewritten:
.PP
.Vb 2
\& package A;
\& sub foo { print "called A::foo\en"; shift->NEXT::DISTINCT::foo() }
.Ve
.PP
.Vb 2
\& package B;
\& sub foo { print "called B::foo\en"; shift->NEXT::DISTINCT::foo() }
.Ve
.PP
.Vb 2
\& package C; @ISA = qw( A );
\& sub foo { print "called C::foo\en"; shift->NEXT::DISTINCT::foo() }
.Ve
.PP
.Vb 2
\& package D; @ISA = qw(A B);
\& sub foo { print "called D::foo\en"; shift->NEXT::DISTINCT::foo() }
.Ve
.PP
.Vb 2
\& package E; @ISA = qw(C D);
\& sub foo { print "called E::foo\en"; shift->NEXT::DISTINCT::foo() }
.Ve
.PP
.Vb 1
\& E->foo();
.Ve
.PP
then it would print:
.PP
.Vb 5
\& called E::foo
\& called C::foo
\& called A::foo
\& called D::foo
\& called B::foo
.Ve
.PP
and omit the second call to \f(CW\*(C`A::foo\*(C'\fR (since it would not be distinct
from the first call to \f(CW\*(C`A::foo\*(C'\fR).
.PP
Note that you can also use:
.PP
.Vb 1
\& $self->NEXT::DISTINCT::ACTUAL::method();
.Ve
.PP
or:
.PP
.Vb 1
\& $self->NEXT::ACTUAL::DISTINCT::method();
.Ve
.PP
to get both unique invocation \fIand\fR exception\-on\-failure.
.PP
Note that, for historical compatibility, you can also use
\&\f(CW\*(C`NEXT::UNSEEN\*(C'\fR instead of \f(CW\*(C`NEXT::DISTINCT\*(C'\fR.
.Sh "Invoking all versions of a method with a single call"
.IX Subsection "Invoking all versions of a method with a single call"
Yet another pseudo-class that \s-1NEXT\s0.pm provides is \f(CW\*(C`EVERY\*(C'\fR.
Its behaviour is considerably simpler than that of the \f(CW\*(C`NEXT\*(C'\fR family.
A call to:
.PP
.Vb 1
\& $obj->EVERY::foo();
.Ve
.PP
calls \fIevery\fR method named \f(CW\*(C`foo\*(C'\fR that the object in \f(CW$obj\fR has inherited.
That is:
.PP
.Vb 1
\& use NEXT;
.Ve
.PP
.Vb 2
\& package A; @ISA = qw(B D X);
\& sub foo { print "A::foo " }
.Ve
.PP
.Vb 2
\& package B; @ISA = qw(D X);
\& sub foo { print "B::foo " }
.Ve
.PP
.Vb 2
\& package X; @ISA = qw(D);
\& sub foo { print "X::foo " }
.Ve
.PP
.Vb 2
\& package D;
\& sub foo { print "D::foo " }
.Ve
.PP
.Vb 1
\& package main;
.Ve
.PP
.Vb 2
\& my $obj = bless {}, 'A';
\& $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
.Ve
.PP
Prefixing a method call with \f(CW\*(C`EVERY::\*(C'\fR causes every method in the
object's hierarchy with that name to be invoked. As the above example
illustrates, they are not called in Perl's usual \*(L"left\-most\-depth\-first\*(R"
order. Instead, they are called \*(L"breadth\-first\-dependency\-wise\*(R".
.PP
That means that the inheritance tree of the object is traversed breadth-first
and the resulting order of classes is used as the sequence in which methods
are called. However, that sequence is modified by imposing a rule that the
appropritae method of a derived class must be called before the same method of
any ancestral class. That's why, in the above example, \f(CW\*(C`X::foo\*(C'\fR is called
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.
.PP
In general, there's no need to worry about the order of calls. They will be
left\-to\-right, breadth\-first, most\-derived\-first. This works perfectly for
most inherited methods (including destructors), but is inappropriate for
some kinds of methods (such as constructors, cloners, debuggers, and
initializers) where it's more appropriate that the least-derived methods be
called first (as more-derived methods may rely on the behaviour of their
\&\*(L"ancestors\*(R"). In that case, instead of using the \f(CW\*(C`EVERY\*(C'\fR pseudo\-class:
.PP
.Vb 1
\& $obj->EVERY::foo(); # prints" A::foo B::foo X::foo D::foo
.Ve
.PP
you can use the \f(CW\*(C`EVERY::LAST\*(C'\fR pseudo\-class:
.PP
.Vb 1
\& $obj->EVERY::LAST::foo(); # prints" D::foo X::foo B::foo A::foo
.Ve
.PP
which reverses the order of method call.
.PP
Whichever version is used, the actual methods are called in the same
context (list, scalar, or void) as the original call via \f(CW\*(C`EVERY\*(C'\fR, and return:
.IP "\(bu" 4
A hash of array references in list context. Each entry of the hash has the
fully qualified method name as its key and a reference to an array containing
the method's list-context return values as its value.
.IP "\(bu" 4
A reference to a hash of scalar values in scalar context. Each entry of the hash has the
fully qualified method name as its key and the method's scalar-context return values as its value.
.IP "\(bu" 4
Nothing in void context (obviously).
.ie n .Sh "Using ""EVERY"" methods"
.el .Sh "Using \f(CWEVERY\fP methods"
.IX Subsection "Using EVERY methods"
The typical way to use an \f(CW\*(C`EVERY\*(C'\fR call is to wrap it in another base
method, that all classes inherit. For example, to ensure that every
destructor an object inherits is actually called (as opposed to just the
left-most-depth-first-est one):
.PP
.Vb 2
\& package Base;
\& sub DESTROY { $_[0]->EVERY::Destroy }
.Ve
.PP
.Vb 3
\& package Derived1;
\& use base 'Base';
\& sub Destroy {...}
.Ve
.PP
.Vb 3
\& package Derived2;
\& use base 'Base', 'Derived1';
\& sub Destroy {...}
.Ve
.PP
et cetera. Every derived class than needs its own clean-up
behaviour simply adds its own \f(CW\*(C`Destroy\*(C'\fR method (\fInot\fR a \f(CW\*(C`DESTROY\*(C'\fR method),
which the call to \f(CW\*(C`EVERY::LAST::Destroy\*(C'\fR in the inherited destructor
then correctly picks up.
.PP
Likewise, to create a class hierarchy in which every initializer inherited by
a new object is invoked:
.PP
.Vb 6
\& package Base;
\& sub new {
\& my ($class, %args) = @_;
\& my $obj = bless {}, $class;
\& $obj->EVERY::LAST::Init(\e%args);
\& }
.Ve
.PP
.Vb 6
\& package Derived1;
\& use base 'Base';
\& sub Init {
\& my ($argsref) = @_;
\& ...
\& }
.Ve
.PP
.Vb 6
\& package Derived2;
\& use base 'Base', 'Derived1';
\& sub Init {
\& my ($argsref) = @_;
\& ...
\& }
.Ve
.PP
et cetera. Every derived class than needs some additional initialization
behaviour simply adds its own \f(CW\*(C`Init\*(C'\fR method (\fInot\fR a \f(CW\*(C`new\*(C'\fR method),
which the call to \f(CW\*(C`EVERY::LAST::Init\*(C'\fR in the inherited constructor
then correctly picks up.
.SH "AUTHOR"
.IX Header "AUTHOR"
Damian Conway (damian@conway.org)
.SH "BUGS AND IRRITATIONS"
.IX Header "BUGS AND IRRITATIONS"
Because it's a module, not an integral part of the interpreter, \s-1NEXT\s0.pm
has to guess where the surrounding call was found in the method
look-up sequence. In the presence of diamond inheritance patterns
it occasionally guesses wrong.
.PP
It's also too slow (despite caching).
.PP
Comment, suggestions, and patches welcome.
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
.Vb 3
\& Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
\& This module is free software. It may be used, redistributed
\& and/or modified under the same terms as Perl itself.
.Ve