Of Moose, Mouse, Moo, and Mo for OO Perl 1

Posted by JD 12/02/2011 at 22:00

As I relearn Perl OO programming, there are a few changes or improvements
that have been added since the last time I was programming in perl full-time. In the old days, we manually wrote our OO code in perl and liked it. We didn’t know any better. These days we have helper modules to remove the need to re-write over and over many of the same OO code elements thanks to Moose, Mouse, Moo and Mo.

Hopefully, my ignorance doesn’t detract from the data below.

Moose – Main OO Classes

This is a full featured OO module that provides setters, getters, inheritance, roles and many more OO features. It is also known for long startup times.

Mouse – Smaller OO Classes

This module claims to be 80% of what Moose provides and significantly faster, more efficient. Wen I started researching Moose and found Mouse, I’d planned to just use it until I outgrew the features and needed everything that Moose provides.

Mouse is a Moose compatible object system. They do warn that Mouse is a little sloppier than Moose, so if you run into weird errors, it could be Mouse causing them. They claim that running s/Mouse/Moose/g on your code will work.

Moo – Even Smaller OO Classes

Minimal Object Orientation

To some folks, even Mouse was too heavy. They wanted something lighter.
Enter Moo. I know almost nothing about Moo, but it is supposed to be lighter than Mouse. Lighter is always better, right?

The Moo cpan page says that Mouse is 3x larger than Moo and that Mouse takes longer to load than most Moo CGI scripts take to run. The also pledge to make it smooth to upgrade to Moose when you need more than minimal features. That’s cool.

Mo – Minimal OO Classes

I’m not sure if Mo was a joke or was seriously trying to be even lighter
than Moo already was. The tagline for Mo is Mo – Micro Objects. Mo is less.

Compatibility

It appears that all of these packages are compatible. That means if you use Mo, and need to switch to Moo, then doing so isn’t too hard. The same applies to Mouse and Moose too. Pick the smallest one you need and you can grow into the more capable modules when needed later.

Feature         Moose   Mouse   Moo     Mo
new             y       y       y       y
extends         y       y       y       y
has             y       y       y       y
does            y       y       y
BUILDARGS                       y
BUILDALL                        y
DESTROY                         y
with            y               y
before          y       y       y
around          y       y       y
after           y       y       y
confess         y       y
blessed         y       y
roles           y       y       y
override        y
super           y
augment         y
inner           y

I hope those lined up correctly. Tables seem to be broke in this markup.
Please check these features yourself.

Performance Differences

I searched for an article that described the differences between all these
different modules and didn’t find anything, however, someone did create a
little object creation benchmark using each which may only be interesting if not really all that useful. The presentation of the performance numbers was less than ideal. I’ll try to do better. Since the constructs used had to be available in Mo as well as Moose, obviously Mo will perform better than Moose. How much better, that is the question. He used the Benchmark perl module with 100K iterations. It is highly likely that I’m not reading the test results correctly.

Larger numbers mean faster object creation and accesses. That’s better.
When I first saw the graph, I felt the need to check the numbers again.
They were correct.

In theory, the manually coded OO methods should have always been the quickest. It didn’t work that way. I figured the packaged versions were optimized by expert OO and perl guys. If you believe these numbers are completely representative, and they my not be, then Moo or Mouse are clearly the best packages for simple method accesses.

Summary

Since I didn’t know anything else, I changed my plans from starting with Mo and growing into the other larger packages as needed, to starting with Moo and striving to never expand beyond what Mouse provides.

  1. Mark Stosberg 08/28/2012 at 02:44

    Thanks for the post.

    Some updates on Moo vs Mouse features.

    Mouse has “with”, “override”, “super”, “augment” and “inner”. For some reason they are not not clearly documented in Mouse.pm, but are easily visible in the source: http://cpansearch.perl.org/src/GFUJI/Mouse-1.02/lib/Mouse.pm

    Mouse also has “subtype” and “meta”, which Moo does not. In Mouse::Object, you’ll also find BUILD, BUILDALL, BUILDARGS, DEMOLISH and DEMOLISHALL. “DESTROY” should not be on the list. It is a standard feature of the Perl 5 OO system.

    Regarding performance, I have an updated benchmark script here, forked from Schwern’s. Scroll down and you’ll see I also looked at load time, as well as what a more-likely real-world scenario where a “small” module might be used, that factors in both the load time, plus some reasonable amount of method calls:

    https://gist.github.com/3431863