Modern objects with Moo

Contents
has
Required, default, lazy
extends
When to use 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.

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.

Next: Testing with Test::More

Article author: Arthur Isaev

Related articles
Perl
Perl course: zero to hero in 50 lessons
What is Perl and where it runs
Install Perl and check the version
Your first Perl script
The debugger as a REPL
Scalars and sigils
Numbers
Strings
Interpolation and quoting
Arrays
push, pop, shift and splice
Hashes
keys, values, exists and delete
Scalar and list context
Conditions and comparison
while, for, next and last
foreach and the default variable
Subroutines
Arguments in @_
References
Nested data structures
Regular expressions: match and anchors
Captures and named groups
Substitution with s///
split and join
grep and map
Sort and the Schwartzian transform
Read and write files
File tests and metadata
Directories and paths
die, warn and eval
Modules and Exporter
CPAN and cpanm
Objects with bless
Modern objects with Moo
Testing with Test::More
strict, warnings and diagnostics
Options with Getopt::Long
Environment and %ENV
system, backticks and pipes
One-liners with -n and -p
CSV and TSV processing
JSON with JSON::PP
HTTP clients with HTTP::Tiny
Sockets and IO::Socket
Databases with DBI
Capstone: todo CLI
Capstone: log report
Performance and profiling
Modern Perl tour
Hero roadmap: the whole course on one page
Install Perl on Windows 11
Install Perl on Ubuntu
Install Perl on Rocky Linux
Install Perl on macOS
Install Perl on FreeBSD
Perl in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: