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

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.

Synopsis

my $algo = mro::get_mro($class); # “dfs” or “c3”

Returns the string "dfs" or "c3". Classes that have never called set_mro or use mro '...' report "dfs" (Perl’s default). Calling on a non-existent class also reports "dfs"; the call does not force the stash into existence.

get_isarev#

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

get_pkg_gen#

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

is_universal#

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

Method cache#

method_changed_in#

Force method-cache invalidation for $class and everything that inherits from it.

invalidate_all_method_caches#

Invalidate the method cache for every package in the interpreter.

next:: dispatch#

nextcan#

The mro::_nextcan primitive that underpins next::method, next::can, and maybe::next::method.

next_can#

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

next_method#

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

maybe_next_method#

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