Modern objects with Moo
has
Moo turns a class into a declaration. Each attribute is one has line with a name, a read mode and optional checks, and the constructor plus accessors are generated for you. Both modules come from CPAN, so the examples below run after cpanm Moo and cpanm Types::Standard; neither is part of the core.
package User; use Moo; has name => (is => 'ro', required => 1); has age => (is => 'rw', default => sub { 0 }); 1;
ro means read-only, rw means the accessor also sets. The new method appears automatically, so the caller writes User->new(name => "Ada").
Required, default, lazy
Declarations can check values and compute them on first use. The isa check rejects a wrong type where the bug happens instead of three calls later.
use Types::Standard qw(Int Str); has email => (is => 'ro', isa => Str, required => 1); has score => (is => 'rw', isa => Int, default => 0);
Lazy attributes are built on first read and cached afterwards, which is perfect for expensive objects such as a database connection. The builder sub holds the code that creates it.
has dbh => (is => 'ro', lazy => 1, builder => sub { my ($self) = @_; return DBI->connect($self->dsn); });
extends
Inheritance is one declaration. The child sees the parent attributes and may add methods or override them; call super to run the parent version first.
package Admin; use Moo; extends 'User'; has role => (is => 'ro', default => 'admin'); sub describe { my ($self) = @_; return $self->name . " (" . $self->role . ")"; } 1;
Moo is small and fast because it avoids a meta-object protocol. Moose is the older, heavier system with roles, type coercions and introspection; Moo classes can be upgraded to Moose later without changing the callers.
When to use Moo
Use Moo when a class is mostly data with a few methods, which describes most application code. Stay with plain bless when the class is tiny, and reach for Moose when you need roles or a plugin ecosystem.
Either way, install the module first: cpanm Moo. Types::Standard comes with Type::Tiny and is the companion for isa checks.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
has name => (is => 'ro')give the class?Answer
A method that returns the attribute but does not change it
rogenerates a read-only accessor, and the constructor also accepts the attribute by name. -
What is the difference between the
roandrwread modes?Answer
rwgives a setter,roonly a getterThe mode names what the accessor may do: read only, or read and write.
-
What does
required => 1on an attribute mean?Answer
The new call must pass the attribute, or the constructor dies
Moo checks the arguments in the generated constructor and reports the missing ones before the object exists.
-
What does a
lazyattribute with abuilderdo?Answer
It builds the value on the first read and caches it
The builder runs when the accessor is first called, and the value is kept for the reads after it.
-
What does
extends 'User'in anAdminclass do?Answer
It makes Admin a subclass that sees the attributes and methods of User
extends records the parent class, and method calls fall back to it when the child has no method of that name.
-
What does this program print?
package User; use Moo; has name => (is => 'ro', required => 1); package main; my $u = User->new(name => "Ada"); print $u->name, "\n";Answer
Ada
The ro accessor returns the value the constructor stored under name.
-
What does this program print?
package User; use Moo; has age => (is => 'rw', default => sub { 0 }); package main; my $u = User->new; $u->age(36); print $u->age, "\n";Answer
36
The default fills the attribute at construction, and the rw accessor then replaces it with 36.
-
What does this program print?
package User; use Moo; has name => (is => 'ro', required => 1); package Admin; use Moo; extends 'User'; has role => (is => 'ro', default => 'admin'); package main; my $a = Admin->new(name => "Ada"); print $a->name, " (", $a->role, ")\n";Answer
Ada (admin)
The child class takes the parent attribute name and adds its own default role, admin.
-
What does this program print?
package User; use Moo; use Types::Standard qw(Int); has age => (is => 'ro', isa => Int); package main; my $u = User->new(age => 36); print $u->age + 1, "\n";Answer
37
The type check passes, and the accessor returns the number, which the program adds to one.
-
A class declares
has name => (is => 'ro', required => 1);and the caller runsUser->new. What happens?Answer
The constructor dies and names the missing required argument
Moo reports the missing required arguments and stops, which is the point of the check.
Article author: Arthur Isaev