Moose

Henry Van Styn

Issue #209, September 2011

Write powerful object-oriented code in a modern and consistent style—in Perl.

Perl has been around for more than 20 years. During that time, it has received its share of both praise and criticism, and lots of misconceptions surround it. Much of this stems from long-outdated notions of what Perl used to be, but have nothing to do with what Perl actually is today.

Perl hasn't been standing still. It's been growing continuously and evolving, and that growth has accelerated dramatically in the past few years. Moose is one of the technologies at the heart of this “Perl Renaissance”, which also includes other exciting projects that have emerged, such as Catalyst and DBIx::Class.

Moose is essentially a language extension for Perl 5 that provides a modern, elegant, fully featured object system. I say “language extension”, but Moose is written in pure Perl, and as you'll see, its syntax is still normal Perl. You don't need to patch Perl itself to use Moose; under the hood, it's just Perl 5.

Because Moose is still just Perl 5, it's fully compatible with all of those wonderful modules on CPAN, regardless of whether they are written in Moose (and most aren't, as CPAN has been around for so long, and Moose is relatively new).

For me, this is still the single biggest reason to choose Perl. Whatever you are trying to accomplish, chances are, there already is a refined module for it on CPAN. This usually means dramatic cuts in total development time, because someone else already has written a lot of your program for you.

And now, with all the modern object-oriented features Moose brings to Perl, you get to have your cake and eat it too.

In this article, I provide an introduction to object-oriented programming in Moose and cover some of Moose's core features with useful examples. To get the most out of this article, you already should be familiar with object-oriented programming concepts, such as classes, objects, methods, attributes, construction and inheritance.

You also need to know Perl—at least the fundamentals. If you don't know Perl, learning it is not very hard to do. At the end of the day, it's just syntax. The good news is you don't need to master Perl by any stretch to start using Moose.

Perl does have its quirks, and Moose doesn't make them all totally go away (and you wouldn't want them all to go away, because a lot of them are really useful). The most important concepts to understand are how Perl references work (the “perlreftut” tutorial is a great place to start—see Resources), and also the basics of working with Scalars, Arrays and Hashes. Also, learn what the fat comma is (=>) if you aren't already familiar with it. Moose makes heavy use of it as an idiom. It's actually not that scary; it's interchangeable with the normal comma (,).

Most of the rest of it you can learn as you go. Normal language stuff like loops, conditionals and operators aren't all that different in Perl than any other language. So give it a shot. I think you'll find it's well worth the investment.

Getting Moose

Chances are you already have a distribution of Perl installed on your system. You at least should have Perl 5.8, but preferably 5.10 or 5.12. Installing Moose from CPAN is an easy task; simply run the following command:

cpan Moose

This should download and install Moose for you, as well as all of Moose's dependencies.

Object-Oriented Perl (the Old Way)

Even though Perl has had object-oriented features for a long time, it was not originally designed—syntactically—as an object-oriented language. This is more about the API provided to the programmer than it is about the underlying technical design of Perl itself.

Perl 5 provides a lean environment with the fundamental features and hooks needed for object-oriented programming, but then leaves most of the details (such as setting up object constructors, implementing attributes and handling validation) to you. As a result, the “right way” to go about implementing these concepts is open to interpretation.

The fundamental feature utilized by Perl to support objects is the “blessed” reference. This is like the flux capacitor of objects in Perl. Blessing simply associates a normal reference (usually a Hash reference) with a class. The blessed reference then becomes the “object instance”, and its referent is used as the container to store the object's data.

The class name is the same thing as the package name, which is nothing more than the namespace in which subroutines and variables are defined. The subroutines defined in the given package namespace become the methods of the class and can be called on the object reference.

All object-oriented languages have to do something along these lines to implement objects under the hood. Other languages just don't impose so many of the low-level details on the programmer as in pure Perl.

Here is an example of a simple class in old-school Perl 5 OO:

package MyApp::Rifle;
use strict;

sub new {
        my ($class, %opts) = @_;
        $opts{rounds} = 0 unless ($opts{rounds});
        my $self = bless( {}, $class );
        $self->rounds($opts{rounds});
        return $self;
}

sub rounds {
        my ($self, $rounds) = @_;
        $self->{_rounds} = $rounds if (defined $rounds);
        return $self->{_rounds};
}

sub fire {
        my $self = shift;
        die "out of ammo!" unless ($self->rounds > 0);
        print "bang!\n";
        $self->rounds( $self->rounds - 1 );
}

1;

Save the above class definition into a file named MyApp/Rifle.pm within one of your Perl's include directories, and then you can use it in a Perl program like this:

use MyApp::Rifle;
use strict;

my $rifle = MyApp::Rifle->new( rounds => 5 );
print "There are " . $rifle->rounds . " rounds in the rifle\n";
$rifle->fire;
print "Now there are " . $rifle->rounds . " rounds in the rifle\n";

Moose Sugar

Moose is really nothing more than syntactic “sugar” that automatically takes care of the boiler-plate tedium and low-level details of implementing objects automatically. This is possible because of Perl's powerful introspection capabilities—Moose dynamically manipulates the class definition at compile time just as if it had been written that way.

The previous class could be implemented like this with Moose:

package MyApp::Rifle;
use Moose;

has 'rounds' => ( is => 'rw', isa => 'Int', default => 0 );

sub fire {
        my $self = shift;
        die "out of ammo!" unless ($self->rounds > 0);
        print "bang!\n";
        $self->rounds( $self->rounds - 1 );
}

1;

Not only is this code much shorter, cleaner and easier to read, but it is doing all the things the non-Moose class was doing and more. First, Moose is automatically creating the “new” constructor method behind the scenes. It is also automatically setting up “rounds” as an attribute (aka object variable), which Moose understands as a distinct concept.

Pure Perl has no such understanding; if you want “attributes”, you have to implement them yourself by writing accessor methods by hand and deciding how they should work (the non-Moose version above illustrates only one of many possible approaches).

Moose, on the other hand, provides a refined, fully integrated object attribute paradigm. It sets up the accessor methods, handles the data storage and retrieval, and automatically configures the constructor to initialize attributes optionally with supplied parameters—and that is just scratching the surface!

One of the problems with the non-Moose version of our class is that there is no validation for “rounds”. For example, nothing stops us from doing this:

my $rifle = MyApp::Rifle->new( rounds => 'foo' );

This is one of the areas where Moose really shines; it provides a complete Type system that is very straightforward to use. In the Moose version, the option isa => 'Int' sets up the rounds attribute with a type constraint that automatically will throw an exception (with a meaningful message) if you try to set the value to anything that is not a valid integer. This would stop you from setting rounds to 'foo' because it's not an integer, it's a string.

This illustrates an important point about Moose's design and approach. Its syntax is declarative rather than imperative. This means you just need to specify what you want it to do instead of how it needs to do it. This is in sharp contrast to the traditional Perl 5 OO style, where that is exactly what you would have to do—add additional lines of code in the accessor method to test the value for validity and handle the result.

The syntax isa => 'Int' doesn't provide any insight on how Moose will go about checking and enforcing the type constraint. And that's the whole point—you don't care. But, you can rest assured it will do it in a far more thorough and robust manner than anything you'd want to waste time on doing yourself.

More about Attributes

You declare attributes in Moose with the “has” function. This consists of a unique attribute name, followed by a list of named options (key/values). Although it looks and behaves like a built-in language keyword, it is really just a function call. Its documented syntax is just idiomatic for the purpose of code readability (it's a fancy way to pass an argument list).

Moose provides all sorts of options that define the behavior of a given attribute, including setup of accessors, data types, initialization and event hooks. The simplest attribute is just an object variable that is set up as either read-write (rw) or read-only (ro) with the “is” option:

has 'first_name' => ( is => 'rw' ); 

The is option tells Moose to set up the accessor method, which is what you use to get and set the attribute's value. You set the value of an attribute by passing a single argument to the accessor (such as $obj->first_name('Fred')), and you get the current value by calling the accessor with no arguments ($obj->first_name). Setting the value is only allowed if the attribute “is” => “rw”. If it's “ro”, and you try to set its value through the accessor an exception will be thrown.

This is the core of the attribute paradigm, but lots of other options provide useful features. Here are a few of the noteworthy ones:

  • is: ro or rw, creates either a read-only or read-write accessor method.

  • isa: string representing an optional type constraint.

  • default: the default value of the attribute.

  • builder: alternative to default—name of a method that will generate the default.

  • lazy: if true, the attribute is not initialized until it's used.

  • required: if true, attribute value must be provided to constructor or have default/builder.

The builder option lets you specify a method to use to populate the attribute's default value. A builder is a normal method defined within the class, and its return value is used to set the attribute's initial value. If the builder needs to access other attributes within the object, the attribute must be lazy (to prevent it from potentially being populated before the other attributes it depends on).

Because this is such a common scenario, for convenience, Moose provides the “lazy_build” attribute option that automatically sets the lazy option and sets the builder to _build_name (such as _build_first_name for an attribute named first_name). For example:

has 'first_name' => ( is => 'ro', lazy_build => 1 );
sub _build_first_name {
        my $self = shift;
        return $self->some_lookup('some data');
}

Attributes Containing Objects

So far, I've talked only about attributes containing simple scalars. Attributes can contain other types of values as well, including references and other objects. For example, you could add a DateTime attribute to your MyApp::Rifle class to keep track of the last time “fire” was called:

package MyApp::Rifle;
use Moose;
use DateTime;

has 'rounds' => ( is => 'rw', isa => 'Int', default => 0 );
has 'fired_dt' => ( is => 'rw', isa => 'DateTime' );

sub fire {
        my $self = shift;
        die "out of ammo!" unless ($self->rounds > 0);
	
        my $dt = DateTime->now( time_zone => 'local' );
        $self->fired_dt($dt);
	
        print "bang!\n";
        print "fired at " . $self->fired_dt->datetime . "\n";
	
        $self->rounds( $self->rounds - 1 );
}

1;

This is fairly straightforward. I'm creating a new DateTime object and storing it in my fired_dt attribute. Then, I can call methods on this object, such as the datetime method, which returns a friendly string representing the date and time.

Delegations

Alternatively, you could utilize Moose's delegation feature when you set up the fired_dt attribute, like this:

has 'fired_dt' => ( 
        is => 'rw', 
        isa => 'DateTime',
        handles => {
                last_fired => 'datetime'
        }
);

This will set up another accessor method named last_fired and map it to the datetime method of the attribute. This makes the invocations of $self->last_fired and $self->fired_dt->datetime equivalent. This is worthwhile because it allows you to keep your API simpler.

Types

Moose provides its own type system for enforcing constraints on the value to which an attribute can be set. As I mentioned earlier, type constraints are set with the isa attribute option.

Moose provides a built-in hierarchy of named types for general-purpose use. For example, Int is a subtype of Num, and Num is a subtype of Str. The value 'foo' would pass Str but not Num or Int; 3.4 would pass Str and Num but not Int, and 7 would pass all of Str, Num and Int.

There also are certain built-in types that can be “parameterized”, such as ArrayRef (a reference to an array). This lets you not only require an attribute to contain an ArrayRef, but also set type constraints on the values that ArrayRef can contain. For example, setting isa => 'ArrayRef[Int]' requires an ArrayRef of Ints. These can be nested multiple levels deep, such as 'ArrayRef[HashRef[Str]]' and so on.

Another special parameterized type is Maybe, which allows a value to be undef. For example, 'Maybe[Num]' means the value is either undef or a Num.

You also can use type “unions”. For example, 'Bool | Ref' means either Bool or Ref.

If the built-in types aren't sufficient for your needs, you can define your own subtypes to do any kind of custom validation you want. The Moose::Util::TypeConstraints documentation provides details on creating subtypes, as well as a complete listing of the built-in types that are available (see Resources).

Finally, instead of specifying the name of a defined type, you can specify a class name, which will require an object of that class type (such as in our DateTime attribute example). All of these concepts can be intermixed for maximum flexibility. So, for example, if you set isa => 'ArrayRef[MyApp::Rifle]', it would require an ArrayRef of MyApp::Rifle objects.

Inheritance

Subclassing is relatively painless in Moose. Use the extends function to make a class a subclass of another. The subclass inherits all the parent's methods and attributes, and then you can define new ones or override existing ones simply by defining them again.

Moose also provides helpful attribute inheritance sugar that allows you to inherit an attribute from the parent, but override specific options in the subclass. To tell Moose to do this, prepend the attribute name with a plus sign (+) in a “has” declaration in the subclass. (Note: attribute options related to accessor method names cannot be changed using this technique.)

For example, you could create a new class named MyApp::AutomaticRifle that inherits from the MyApp::Rifle class from the previous example:


package MyApp::AutomaticRifle;
use Moose;
extends 'MyApp::Rifle';

has '+rounds' => ( default => 50 );
has 'last_burst_num' => ( is => 'rw', isa => 'Int' );

sub burst_fire {
        my ($self, $num) = @_;
	
        $self->last_burst_num($num);

        for (my $i=0; $i<$num; $i++) {
                $self->fire;
        }
}

1;

Here, MyApp::AutomaticRifle can do everything MyApp::Rifle can do, but it also can “burst_fire”. Also, the default of the rounds attribute has been changed to 50 in AutomaticRifle, but the rest of the options for the rounds attribute still are inherited from the parent Rifle class.

You might use MyApp::AutomaticRifle like this:

use strict;
use MyApp::AutomaticRifle;

my $rifle = MyApp::AutomaticRifle->new;
print "There are " . $rifle->rounds . " rounds in the rifle\n";
$rifle->burst_fire(35);
print "Now there are " . $rifle->rounds . " rounds in the rifle\n";

The BUILD Method

Although Moose automatically sets up the “new” constructor for you, there still are times when you need to execute custom code at construction. If you need to do that, define a method named BUILD, and it will be called immediately after the object has been constructed. Don't create a “new” method; that will interfere with Moose's operation.

BUILD is also special as it relates to inheritance. Unlike normal methods that override the parents' methods when redefined in subclasses, BUILD can be defined in every class in the inheritance tree and every one will be called, in order from parent to child.

Roles

Roles define some set of behaviors (attributes and methods) without being full-blown classes themselves (capable of instantiation as objects directly). Instead, Roles are “composed” into other classes, applying the defined behaviors to those classes. Roles are conceptually similar to “mixins” in Ruby.

Roles also can require that consuming classes have certain methods by calling the “requires” sugar function in the Role definition (or throw an exception).

You call the “with” sugar function to consume a Role by name, just like you call “extends” to inherit from a regular class.

Here is an example of a simple Role that could be composed into either MyApp::Rifle or MyApp::AutomaticRifle:

package MyApp::FireAll;
use strict;
use Moose::Role;

requires 'fire', 'rounds';

sub fire_all {
        my $self = shift;
        $self->fire while($self->rounds > 0);
}

1;

You would then add this single line to MyApp::Rifle or MyApp::AutomaticRifle to give either class the fire_all method:

with 'MyApp::FireAll';

In the case of MyApp::AutomaticRifle, the with statement must be called after the extends statement, because the “fire” and “rounds” methods don't exist within MyApp::AutomaticRifle before that, and the Role's requires statements would fail.

If you add the Role to MyApp::Rifle, it will be inherited by MyApp::AutomaticRifle automatically, so there would be no need to add it there also (although it won't break anything if you do).

Method Modifiers

Method modifiers are one of the more powerful and flexible features of Moose. The most common types of modifiers are before, after and around. Before and after are really just “hooks” to execute some code whenever a given method is called, either before or after, as the names imply. For example, this would print a string every time fire_all is called:

before 'fire_all' => sub {
        my $self = shift;
        print "Say hello to my little friend!\n";
};

The “around” modifier is quite a bit more powerful than before and after because it actually can change the arguments passed to, and the data returned from, the original method. It also can programmatically decide whether even to call the original method at all.

Around modifiers actually replace the original method, but get passed the original method and arguments to be able to call it within the new modifier function, but unlike before and after, this has to be done manually in around. The basic blueprint of this is below, which is an example of an around modifier that exactly reproduces the original method (having no observable effect):

around 'fire_all' => sub {
       my ($orig, $self, @args) = @_;
       return $self->$orig(@args);
};

In an around modifier, the first argument is the method ($orig) instead of the object reference ($self) like in normal methods. Then, it's up to you to call the original method ($self->$orig) and capture its return value (or not) and then return.

Method modifiers make a great fit with Roles to define behaviors at a fine-grained level. Let's take a look at another example of a Role for our MyApp::Rifle class that makes use of method modifiers:


package MyApp::MightJam;
use Moose::Role;
use Moose::Util::TypeConstraints;

requires 'fire';

subtype 'Probability' => (
        as 'Num',
        where { $_ >= 0 && $_ <= 1 },
        message { "$_ is not a number between 0 and 1" }
);

has 'jam_probability' => ( 
        is => 'ro', 
        isa => 'Probability', 
        default => .01 
);

sub roll_dice {
        my $self = shift;
        return 1 if ( rand(1) < $self->jam_probability );
        return 0;
}

before 'fire' => sub {
        my $self = shift;
        die "Jammed!!!\n" if ($self->roll_dice);
};

1;

This Role adds the random chance of “Jamming” on any given call to “fire” depending on the probability specified in the jam_probability attribute (with the default probability set to 1%). I also illustrate here how to create a custom subtype, by defining a new type “Probability”, which must be a number between 0 and 1.

You then could compose simple subclasses like the following:

package MyApp::CrappyRifle;
use strict;
use Moose;
extends 'MyApp::AutomaticRifle';
with 'MyApp::MightJam';

has '+jam_probability' => ( default => .5 );

1;

And:

package MyApp::NiceRifle;
use strict;
use Moose;
extends 'MyApp::AutomaticRifle';
with 'MyApp::MightJam';

has '+jam_probability' => ( default => .001 );

1;

The difference between these two is that CrappyRifle will jam on average 5 out 10 times, and NiceRifle will jam only 1 per 1,000 times.

Learning More

This article is just meant as an introduction to Moose, and because of space constraints, I have been able to cover only a few of its core features.

One of the other great things about Moose, and Perl in general, is the community and availability of documentation and resources. The Moose Manual, available on CPAN (see Resources), is well-written and comprehensive. There are also plenty of other docs and information available, and the number of them is growing every day as Moose continues to gain popularity.

If you get stuck on something and can't find the answer, try the #moose IRC channel on irc.perl.org. Many of the top experts are in this channel and are more than willing to help and answer questions. Although they will expect you to RTFM and have done your homework first, they will get you unstuck and pointed in the right direction.

If nothing else, I hope that this article has at least piqued your interest in modern development with Perl and Moose, and that you can see that Perl code can, in fact, be clean, easy to read and modern, while still being “Perlish” and powerful.

As you learn Moose, and modern Perl in general, be sure to check out some of the other projects and modules that are available, including Catalyst, Template::Toolkit, DBIx::Class, Try::Tiny, Test::More and Devel::NYTProf, just to name a few. You might be surprised what's out there, and what is really possible with Perl today.

Henry Van Styn is the founder of IntelliTree Solutions, an IT consulting and software development firm located in Cincinnati, Ohio. Henry has been developing software and solutions for more than ten years, ranging from sophisticated Web applications to low-level network and system utilities. He is the author of Strong Branch Linux, an in-house server distribution based on Gentoo. Henry can be contacted at www.intellitree.com.