mro#

📦 min

Method Resolution Order - how Perl decides which parent class’s method a call inherits.

When a method is called on an object, Perl walks a list of classes looking for a matching subroutine. That list - the linearized MRO - is derived from @ISA and from the algorithm currently in effect for the class. The mro module lets you pick the algorithm, inspect the resulting class list, query the reverse @ISA relation, and manage the method cache.

Two algorithms are available. dfs (depth-first search) is the default and matches Perl’s historical behaviour. c3 produces the C3 linearization familiar from Python, Dylan, and Raku, which keeps subclasses ahead of their ancestors even under diamond inheritance. Pick c3 when multiple inheritance makes the dfs order surprising; otherwise leave the default alone.

Loading the module also enables next::method, next::can, and maybe::next::method - the C3-aware cousins of SUPER::. They always walk the C3 linearization regardless of which MRO the class itself uses.

Synopsis#

use mro;                 # enables next::method, next::can, maybe::next::method

use mro 'c3';            # set C3 linearization for the current package
use mro 'dfs';           # set depth-first (Perl's default) explicitly

my $order = mro::get_linear_isa('MyClass');   # arrayref of class names
my $algo  = mro::get_mro('MyClass');          # "dfs" or "c3"
mro::set_mro('MyClass', 'c3');                # set per-class at runtime

next:: dispatch#

These are mro.pm’s own Perl subs, unchanged:

  • $self->next::method(@args) - jumps (as goto &sub) to the next implementation of the current method in the C3 linearization of $self’s class, in the caller’s context; dies with No next::method 'NAME' found for CLASS when there is none.

  • $self->maybe::next::method(@args) - the same, but returns empty when there is no next method.

  • $self->next::can - a code reference to the next method, or undef.

All three must be called from inside a named method; elsewhere they die with next::method/next::can/maybe::next::method must be used in method context.

This is a native implementation of the XS half of mro 1.30; its mro.pm is used unchanged.

Differences from upstream#

None.

Functions#

MRO selection#

set_mro#

Set the method-resolution algorithm for $class to either "dfs" or "c3".

MRO introspection#

get_linear_isa#

Return the full ordered list of classes Perl would search for a method call on $class, starting with $class itself.

get_mro#

Report which method-resolution algorithm is currently in effect for $class.

get_isarev#

List every class that inherits from $class, directly or transitively, via @ISA.

is_universal#

Report whether $class is UNIVERSAL itself or one of its ancestors via @ISA.

get_pkg_gen#

Return a counter that increments every time a local method in $class changes or its local @ISA is modified.

Method cache#

invalidate_all_method_caches#

Invalidate the method cache for every package in the interpreter.

Other Functions#

next::method#

Tail-call the next implementation of the currently executing method, passing the original arguments.

next::can#

Return a code reference to the next implementation of the currently executing method, or undef if there is none.

maybe::next::method#

Tail-call the next implementation of the currently executing method if one exists; otherwise return quietly.