Skip to content

Perl Moose Guide — Modern OOP Framework with Type System and Roles

DodaTech Updated 2026-06-28 3 min read

In this tutorial, you will learn about Perl Moose Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl Moose replaces manual bless-based OOP with declarative attributes (has), inheritance (extends), roles (with), and method modifiers (before, after, around) -- including built-in type validation, default values, and lazy builders.

Basic Moose Class

package Person;
use Moose;

has 'name' => (
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);

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

has 'full_name' => (
    is       => 'ro',
    lazy     => 1,
    builder  => '_build_full_name',
);

sub _build_full_name {
    my $self = shift;
    return $self->name;  # extend for first/last
}

no Moose;
__PACKAGE__->meta->make_immutable;

Using Moose Class

use Person;

my $alice = Person->new(name => "Alice", age => 30);
print $alice->name;    # Alice
$alice->age(31);       # setter
print $alice->age;     # 31

Inheritance with Moose

package Employee;
use Moose;
extends 'Person';

has 'position' => (
    is       => 'rw',
    isa      => 'Str',
    default  => 'Staff',
);

has 'salary' => (
    is       => 'rw',
    isa      => 'Num',
    required => 1,
);

sub promote {
    my $self = shift;
    $self->position('Senior ' . $self->position);
    $self->salary($self->salary * 1.15);
}

no Moose;
__PACKAGE__->meta->make_immutable;

Roles (Mixins)

package Loggable;
use Moose::Role;

requires 'log_prefix';

sub log {
    my ($self, $msg) = @_;
    print "[" . $self->log_prefix . "] $msg\n";
}

package Service;
use Moose;
with 'Loggable';

has 'name' => (is => 'rw', isa => 'Str');
sub log_prefix { shift->name }

my $svc = Service->new(name => 'API');
$svc->log("Started");  # [API] Started

Method Modifiers

package SecurePerson;
use Moose;
extends 'Person';

before 'name' => sub {
    my $self = shift;
    die "Read-only" if @_;  # prevent setting
};

after 'age' => sub {
    my $self = shift;
    if ($self->age >= 65) {
        print "Retirement age reached\n";
    }
};

around 'greet' => sub {
    my $orig = shift;
    my $self = shift;
    return "[SECURE] " . $self->$orig(@_);
};

Common Mistakes

1. Forgetting no Moose

Always call no Moose at the end of the class and __PACKAGE__->meta->make_immutable for performance.

2. Not specifying is

Every attribute needs is => 'ro' (read-only) or is => 'rw' (read-write). Without it, no accessor is created.

3. Overusing required

Use default or Builder instead of required when possible. required should be for truly mandatory constructor args.

Practice Questions

1. What does has do in Moose? Declares an attribute with options: is (accessor type), isa (type constraint), default/builder (initial value).

2. What is a role in Moose? A composable unit of behavior (like interfaces + implementation). Classes use roles with the with keyword.

3. What does make_immutable do? Optimizes the class by making it immutable -- improving performance and preventing runtime changes to the metaclass.

FAQ

{{< faq question="Is Moose still maintained?" >}} Yes, Moose is stable and maintained. For new projects, Moose and Moo (lightweight Moose) are both active. {{< /faq >}}

{{< faq question="What is the difference between Moose and Moo?" >}} Moo is a lighter, faster Moose-compatible alternative with no dependencies. It supports most Moose features with fewer compile-time penalties. {{< /faq >}}

{{< faq question="Can I use Moose with existing Perl code?" >}} Yes, Moose classes can interoperate with traditional bless-based classes. You can gradually migrate from bless to Moose. {{< /faq >}}

What's Next

Now learn about scalar and list context in Perl.

Topic Description Link
Context Scalar and list context {{< ref "17-context" >}}
String Functions Advanced string handling {{< ref "18-string-functions" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro