TTY Internet Solutions


mro - Method Resolution Order


  use mro;


The "mro" namespace provides several utilities for dealing with method resolution order and method caching in general.


It's possible to change the MRO of a given class either by using use mro as shown in the synopsis, or by using the "mro::set_mro" function below. The functions in the mro namespace do not require loading the mro module, as they are actually provided by the core perl interpreter.

The special methods next::method, next::can, and maybe::next::method are not available until this mro module has been loaded via use or require.

The C3 MRO

C3 is the defualt MRO of Perl Kurila, in contrast to Perl 5 which has depth first search (DFS) as default MRO. Perl's support for C3 is based on the work done in Stevan Little's module Class::C3, and most of the C3-related documentation here is ripped directly from there.

What is C3?

C3 is the name of an algorithm which aims to provide a sane method resolution order under multiple inheritance. It was first introduced in the language Dylan (see links in the "SEE ALSO" section), and then later adopted as the preferred MRO (Method Resolution Order) for the new-style classes in Python 2.3. Most recently it has been adopted as the "canonical" MRO for Perl 6 classes, and the default MRO for Parrot objects as well.

How does C3 work

C3 works by always preserving local precendence ordering. This essentially means that no class will appear before any of its subclasses. Take, for instance, the classic diamond inheritance pattern:

    /   \
  <B>   <C>
    \   /

The standard Perl 5 MRO would be (D, B, A, C). The result being that A appears before C, even though C is the subclass of A. The C3 MRO algorithm however, produces the following order: (D, B, C, A), which does not have this issue.

This example is fairly trivial; for more complex cases and a deeper explanation, see the links in the "SEE ALSO" section.


mro::get_linear_isa($classname[, $type])

Returns an arrayref which is the linearized MRO of the given class. Uses whichever MRO is currently in effect for that class by default, or the given MRO (either c3 or dfs if specified as $type).

The linearized MRO of a class is an ordered array of all of the classes one would search when resolving a method on that class, starting with the class itself.

If the requested class doesn't yet exist, this function will still succeed, and return [ $classname ]

Note that UNIVERSAL (and any members of UNIVERSAL's MRO) are not part of the MRO of a class, even though all classes implicitly inherit methods from UNIVERSAL and its parents.

mro::set_mro($classname, $type)

Sets the MRO of the given class to the $type argument (either c3 or dfs).


Returns the MRO of the given class (either c3 or dfs).


Gets the mro_isarev for this class, returned as an arrayref of class names. These are every class that "isa" the given class name, even if the isa relationship is indirect. This is used internally by the MRO code to keep track of method/MRO cache invalidations.

Currently, this list only grows, it never shrinks. This was a performance consideration (properly tracking and deleting isarev entries when someone removes an entry from an @ISA is costly, and it doesn't happen often anyways). The fact that a class which no longer truly "isa" this class at runtime remains on the list should be considered a quirky implementation detail which is subject to future change. It shouldn't be an issue as long as you're looking at this list for the same reasons the core code does: as a performance optimization over having to search every class in existence.

As with mro::get_mro above, UNIVERSAL is special. UNIVERSAL (and parents') isarev lists do not include every class in existence, even though all classes are effectively descendants for method inheritance purposes.


Returns a boolean status indicating whether or not the given classname is either UNIVERSAL itself, or one of UNIVERSAL's parents by @ISA inheritance.

Any class for which this function returns true is "universal" in the sense that all classes potentially inherit methods from it.

For similar reasons to isarev above, this flag is permanent. Once it is set, it does not go away, even if the class in question really isn't universal anymore.


Increments PL_sub_generation, which invalidates method caching in all packages.


Invalidates the method cache of any classes dependent on the given class. This is not normally necessary. The only known case where pure perl code can confuse the method cache is when you manually install a new constant subroutine by using a readonly scalar value, like the internals of constant do. If you find another case, please report it so we can either fix it or document the exception here.


Returns an integer which is incremented every time a real local method in the package $classname changes, or the local @ISA of $classname is modified.

This is intended for authors of modules which do lots of class introspection, as it allows them to very quickly check if anything important about the local properties of a given class have changed since the last time they looked. It does not increment on method/@ISA changes in superclasses.

It's still up to you to seek out the actual changes, and there might not actually be any. Perhaps all of the changes since you last checked cancelled each other out and left the package in the state it was in before.

This integer normally starts off at a value of 1 when a package stash is instantiated. Calling it on packages whose stashes do not exist at all will return 0. If a package stash is completely deleted (not a normal occurence, but it can happen if someone does something like undef %PkgName::), the number will be reset to either 0 or 1, depending on how completely package was wiped out.


This is similar to next::method, but just returns either a code reference or undef to indicate that no further methods of this name exist.


In simple cases, it is equivalent to:

   $self->next::method(@_) if $self->next_can;

But there are some cases where only this solution works (like goto &maybe::next::method);


The original Dylan paper

The prototype Perl 6 Object Model uses C3

Parrot now uses C3

C3 for TinyCLOS




Brandon L. Black, <>

Based on Stevan Little's Class::C3