The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::Multi - Multiple inheritance support functions.

SYNOPSIS

  • A flexible inheritance traversal function.

  • A calling-class-relative metaphor of can().

  • A calling-class-relative metaphor of SUPER::.

Inheritance Traversal Function

walk_depth( \&testsub, CLASS, @avoid )

walk_width( \&testsub, CLASS, @avoid )

walk_width_up( \&testsub, CLASS, @avoid )

Executes the supplied code reference once for each superclass of the supplied derived class, until the code reference returns true.

walk_depth uses the same depth-first search order that Perl uses internally.

walk_width uses a breadth-first search order that is more appropriate for diamond inheritance situations. Thus, and also since Perl already provides methods that use depth-first, all of the other functions in this package use the breadth-first search order.

walk_width_up uses breadth-first, but starts with the base class and works its way toward the derived. This is more appropriate for a constructive purpose where the base class should initialize first, where walk_width is more appropriate for a destructive purpose where the base class should be called last.

If an @avoid list is supplied, the code reference will not be executed until all classes in that list have been seen, whichever direction it's going.

walk_width { BLOCK } $derived

Executes the { BLOCK } once each for $derived and its superclasses.

walk_width { BLOCK } $derived, $derived

Executes the { BLOCK } only for $derived's superclasses.

walk_width { BLOCK } $derived, __PACKAGE__

Executes the { BLOCK } only for classes that are inherited after the class in which the expression is found.

Multi-Inherited Method Search

other( $this, METHOD )

other checks if the object or class $this has a method called 'METHOD', that occurs -AFTER- the calling class in the inheritance tree.

Usage and semantics are otherwise identical to UNIVERSAL::can

The calling class is inferred via caller().

otherpkg( $this, METHOD )

Identical to other, except the package name is returned instead of the desired method's code reference.

otherrun( $this, METHOD, @myargs )

Identical to other, except the function is run and its result returned instead of the desired method's code reference.

Equivalent to &{other( $this, METHOD )}( $this, @myargs );.

Multi-Inherited Breadth-First Chain-Up Call

$this->OTHER::mymethod( @myargs ); =head2 $this->OTHER::MAY::mymethod( @myargs ); =head2 $this->OTHER::MAY::UP::mymethod( @myargs ); =head2 $this->OTHER::UP::mymethod( @myargs );

Syntactic sugar.

Equivalent to &{other( $this, 'mymethod' )}( $this, @myargs );.

Like SUPER::, OTHER:: expects the requested method to exist. If it does not, an exception is thrown.

See next section for an explanation of the flags. When OTHER is used without ALL, the behavior of HERE is implied.

Multi-Inherited Iterative Method Call

$this->OTHER::ALL::mymethod( @myargs ); =head2 $this->OTHER::ALL::MAY::mymethod( @myargs ); =head2 $this->OTHER::ALL::MAY::UP::mymethod( @myargs ); =head2 $this->OTHER::ALL::UP::mymethod( @myargs );

$this->OTHER::ALL::HERE::mymethod( @myargs ); =head2 $this->OTHER::ALL::HERE::MAY::mymethod( @myargs ); =head2 $this->OTHER::ALL::HERE::MAY::UP::mymethod( @myargs ); =head2 $this->OTHER::ALL::HERE::UP::mymethod( @myargs );

Syntactic sugar.

Calls every implementation of the requested method in all of $this's base classes. The method must exist at least once.

If you do not want an exception to be thrown if no method is found, use the MAY modifier.

If you want the subclasses to be called from the base to the derived, rather than the normal derived-to-base order, use the UP modifier.

If you want the subclasses to be searched starting from the one that made the call, rather than $this (or $this's deepest base class, if UP is also given), use the HERE modifier.

If you do not want to provide an empty method in your base class to satisfy that, and don't like the ::MAY syntax, then you can wrap an OTHER::ALL invocation in an eval { } call. But, remember to localize @_ before you do so.

AUTHORS

Kevin Cody-Little <kcody@cpan.org>