Jump to content

User:EvanCarroll/Moose/Mro

From Wikibooks, open books for an open world

Perl (prior to the yet unreleased 5.10) has a very illogical Method Resolution Order (MRO), this becomes apparent when using multiple inheritance dispatching. A fix for MI-dispatching was first popularized with Dameon's NEXT, which turned out to be a buggy hack that failed horribly in many cases. NEXT was unfortunately inducted into the CORE distro with perl v5.7.3 and remains in the CORE even day. Moose uses the newer jazzier C optimized Class::C3, and is the new standard in all perl's greater than Perl 5.95.[1]

Before Class::C3 and NEXT, you had SUPER which predates NEXT by approximately six months and thankfully never made CORE. SUPER was goofy and mostly useless because if foo and bar were siblings and subclasses of baz you could not re-dispatch in foo to a method in bar.

Essentially SUPER supported Method Dispatch only for linear patterns. NEXT supported them in a fashion that was quirky and made little sense.[2] Without learning the quirks there is a high probability the new Class::C3 does precisely what the programmer wants - it is intuitive. Class::C3 is also employed by Python.

The old way

[edit | edit source]

The differences between NEXT and Class::C3 are outside of the range of this book; but, because vanilla perl doesn't even permit method resolution, and dispatch in the sense of the OO paradigm we will show NEXT in its stead.

An Example

[edit | edit source]
package A;
use strict;
use warnings;
use NEXT;

sub rant { print "ha" }

package B
use strict;
use warnings;
use NEXT;

our @ISA = 'A'

package C
use strict;
use NEXT;
our @ISA = 'B'

sub rant { print "sucks" }

package main

Invoking the Moose

[edit | edit source]

An Example

[edit | edit source]
[edit | edit source]

Footnotes

[edit | edit source]
  1. ^ Perl 5.95 adds a new pragma mro which uses C3 resolution. This pragma is however opt in and makes a few tools available to you, which can all be utilized maintaining reverse compatibility with older perls using MRO::Compat.
  2. ^ The method that made little sense is called Depth First Search (DFS). DFS was the predecessor MRO to C3 in perl. Briefly, it did not handle any form of triangle inheritance in a sane fashion.