24
Jun/09
2

use Moose;

If you’re used to regular OO perl you’ll recognize this:

package Dog;
use strict;
use warnings;

sub new {
    my ($class, %args) = @_;
    $args{name} ||= 'Spot';
    return bless \%args, shift;
}

sub bark {
    my ($self) = @_;
    print $self->{name} . " barks!\n";
}

1;

With Moose this becomes drastically simpler and more powerful:

package Cat;
use Moose;

has 'name' => (is=>'ro', isa=>'Str', default=>'Fluffy');

sub meow {
    my ($self) = @_;
    print $self->name() . " meows!\n";
}

1;

You may hear that Moose is slow. Don’t let that throw you off, it only adds a fraction of a second to startup time, and is exceptionally fast during run-time. There are much more important things to be concerned about. Moose will make your code cleaner, easier to change and extend, and makes coding perl oo quite enjoyable.

Tagged as: ,