.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Moo 3"
.TH Moo 3 "2021-03-29" "perl v5.26.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Moo \- Minimalist Object Orientation (with Moose compatibility)
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& package Cat::Food;
\&
\& use Moo;
\& use strictures 2;
\& use namespace::clean;
\&
\& sub feed_lion {
\& my $self = shift;
\& my $amount = shift || 1;
\&
\& $self\->pounds( $self\->pounds \- $amount );
\& }
\&
\& has taste => (
\& is => \*(Aqro\*(Aq,
\& );
\&
\& has brand => (
\& is => \*(Aqro\*(Aq,
\& isa => sub {
\& die "Only SWEET\-TREATZ supported!" unless $_[0] eq \*(AqSWEET\-TREATZ\*(Aq
\& },
\& );
\&
\& has pounds => (
\& is => \*(Aqrw\*(Aq,
\& isa => sub { die "$_[0] is too much cat food!" unless $_[0] < 15 },
\& );
\&
\& 1;
.Ve
.PP
And elsewhere:
.PP
.Vb 5
\& my $full = Cat::Food\->new(
\& taste => \*(AqDELICIOUS.\*(Aq,
\& brand => \*(AqSWEET\-TREATZ\*(Aq,
\& pounds => 10,
\& );
\&
\& $full\->feed_lion;
\&
\& say $full\->pounds;
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\f(CW\*(C`Moo\*(C'\fR is an extremely light-weight Object Orientation system. It allows one to
concisely define objects and roles with a convenient syntax that avoids the
details of Perl's object system. \f(CW\*(C`Moo\*(C'\fR contains a subset of Moose and is
optimised for rapid startup.
.PP
\&\f(CW\*(C`Moo\*(C'\fR avoids depending on any \s-1XS\s0 modules to allow for simple deployments. The
name \f(CW\*(C`Moo\*(C'\fR is based on the idea that it provides almost \*(-- but not quite \*(--
two thirds of Moose. As such, the Moose::Manual can serve as an effective
guide to \f(CW\*(C`Moo\*(C'\fR aside from the \s-1MOP\s0 and Types sections.
.PP
Unlike Mouse this module does not aim at full compatibility with
Moose's surface syntax, preferring instead to provide full interoperability
via the metaclass inflation capabilities described in \*(L"\s-1MOO AND MOOSE\*(R"\s0.
.PP
For a full list of the minor differences between Moose and Moo's surface
syntax, see \*(L"\s-1INCOMPATIBILITIES WITH MOOSE\*(R"\s0.
.SH "WHY MOO EXISTS"
.IX Header "WHY MOO EXISTS"
If you want a full object system with a rich Metaprotocol, Moose is
already wonderful.
.PP
But if you don't want to use Moose, you may not want \*(L"less metaprotocol\*(R"
like Mouse offers, but you probably want \*(L"no metaprotocol\*(R", which is what
Moo provides. \f(CW\*(C`Moo\*(C'\fR is ideal for some situations where deployment or startup
time precludes using Moose and Mouse:
.IP "a command line or \s-1CGI\s0 script where fast startup is essential" 2
.IX Item "a command line or CGI script where fast startup is essential"
.PD 0
.IP "code designed to be deployed as a single file via App::FatPacker" 2
.IX Item "code designed to be deployed as a single file via App::FatPacker"
.IP "a \s-1CPAN\s0 module that may be used by others in the above situations" 2
.IX Item "a CPAN module that may be used by others in the above situations"
.PD
.PP
\&\f(CW\*(C`Moo\*(C'\fR maintains transparent compatibility with Moose so if you install and
load Moose you can use Moo classes and roles in Moose code without
modification.
.PP
Moo \*(-- Minimal Object Orientation \*(-- aims to make it smooth to upgrade to
Moose when you need more than the minimal features offered by Moo.
.SH "MOO AND MOOSE"
.IX Header "MOO AND MOOSE"
If Moo detects Moose being loaded, it will automatically register
metaclasses for your Moo and Moo::Role packages, so you should be able
to use them in Moose code without modification.
.PP
Moo will also create Moose type constraints for
Moo classes and roles, so that in Moose classes \f(CW\*(C`isa => \*(AqMyMooClass\*(Aq\*(C'\fR
and \f(CW\*(C`isa => \*(AqMyMooRole\*(Aq\*(C'\fR work the same as for Moose classes and roles.
.PP
Extending a Moose class or consuming a Moose::Role will also work.
.PP
Extending a Mouse class or consuming a Mouse::Role will also work. But
note that we don't provide Mouse metaclasses or metaroles so the other way
around doesn't work. This feature exists for Any::Moose users porting to
Moo; enabling Mouse users to use Moo classes is not a priority for us.
.PP
This means that there is no need for anything like Any::Moose for Moo
code \- Moo and Moose code should simply interoperate without problem. To
handle Mouse code, you'll likely need an empty Moo role or class consuming
or extending the Mouse stuff since it doesn't register true Moose
metaclasses like Moo does.
.PP
If you need to disable the metaclass creation, add:
.PP
.Vb 1
\& no Moo::sification;
.Ve
.PP
to your code before Moose is loaded, but bear in mind that this switch is
global and turns the mechanism off entirely so don't put this in library code.
.SH "MOO AND CLASS::XSACCESSOR"
.IX Header "MOO AND CLASS::XSACCESSOR"
If a new enough version of Class::XSAccessor is available, it will be used
to generate simple accessors, readers, and writers for better performance.
Simple accessors are those without lazy defaults, type checks/coercions, or
triggers. Simple readers are those without lazy defaults. Readers and writers
generated by Class::XSAccessor will behave slightly differently: they will
reject attempts to call them with the incorrect number of parameters.
.SH "MOO VERSUS ANY::MOOSE"
.IX Header "MOO VERSUS ANY::MOOSE"
Any::Moose will load Mouse normally, and Moose in a program using
Moose \- which theoretically allows you to get the startup time of Mouse
without disadvantaging Moose users.
.PP
Sadly, this doesn't entirely work, since the selection is load order dependent
\&\- Moo's metaclass inflation system explained above in \*(L"\s-1MOO AND MOOSE\*(R"\s0 is
significantly more reliable.
.PP
So if you want to write a \s-1CPAN\s0 module that loads fast or has only pure perl
dependencies but is also fully usable by Moose users, you should be using
Moo.
.PP
For a full explanation, see the article
<https://shadow.cat/blog/matt\-s\-trout/moo\-versus\-any\-moose> which explains
the differing strategies in more detail and provides a direct example of
where Moo succeeds and Any::Moose fails.
.SH "PUBLIC METHODS"
.IX Header "PUBLIC METHODS"
Moo provides several methods to any class using it.
.SS "new"
.IX Subsection "new"
.Vb 1
\& Foo::Bar\->new( attr1 => 3 );
.Ve
.PP
or
.PP
.Vb 1
\& Foo::Bar\->new({ attr1 => 3 });
.Ve
.PP
The constructor for the class. By default it will accept attributes either as a
hashref, or a list of key value pairs. This can be customized with the
\&\*(L"\s-1BUILDARGS\*(R"\s0 method.
.SS "does"
.IX Subsection "does"
.Vb 3
\& if ($foo\->does(\*(AqSome::Role1\*(Aq)) {
\& ...
\& }
.Ve
.PP
Returns true if the object composes in the passed role.
.SS "\s-1DOES\s0"
.IX Subsection "DOES"
.Vb 3
\& if ($foo\->DOES(\*(AqSome::Role1\*(Aq) || $foo\->DOES(\*(AqSome::Class1\*(Aq)) {
\& ...
\& }
.Ve
.PP
Similar to \*(L"does\*(R", but will also return true for both composed roles and
superclasses.
.SS "meta"
.IX Subsection "meta"
.Vb 2
\& my $meta = Foo::Bar\->meta;
\& my @methods = $meta\->get_method_list;
.Ve
.PP
Returns an object that will behave as if it is a
Moose metaclass object for the class. If you call
anything other than \f(CW\*(C`make_immutable\*(C'\fR on it, the object will be transparently
upgraded to a genuine Moose::Meta::Class instance, loading Moose in the
process if required. \f(CW\*(C`make_immutable\*(C'\fR itself is a no-op, since we generate
metaclasses that are already immutable, and users converting from Moose had
an unfortunate tendency to accidentally load Moose by calling it.
.SH "LIFECYCLE METHODS"
.IX Header "LIFECYCLE METHODS"
There are several methods that you can define in your class to control
construction and destruction of objects. They should be used rather than trying
to modify \f(CW\*(C`new\*(C'\fR or \f(CW\*(C`DESTROY\*(C'\fR yourself.
.SS "\s-1BUILDARGS\s0"
.IX Subsection "BUILDARGS"
.Vb 2
\& around BUILDARGS => sub {
\& my ( $orig, $class, @args ) = @_;
\&
\& return { attr1 => $args[0] }
\& if @args == 1 && !ref $args[0];
\&
\& return $class\->$orig(@args);
\& };
\&
\& Foo::Bar\->new( 3 );
.Ve
.PP
This class method is used to transform the arguments to \f(CW\*(C`new\*(C'\fR into a hash
reference of attribute values.
.PP
The default implementation accepts a hash or hash reference of named parameters.
If it receives a single argument that isn't a hash reference it will throw an
error.
.PP
You can override this method in your class to handle other types of options
passed to the constructor.
.PP
This method should always return a hash reference of named options.
.SS "\s-1FOREIGNBUILDARGS\s0"
.IX Subsection "FOREIGNBUILDARGS"
.Vb 4
\& sub FOREIGNBUILDARGS {
\& my ( $class, $options ) = @_;
\& return $options\->{foo};
\& }
.Ve
.PP
If you are inheriting from a non-Moo class, the arguments passed to the parent
class constructor can be manipulated by defining a \f(CW\*(C`FOREIGNBUILDARGS\*(C'\fR method.
It will receive the same arguments as \*(L"\s-1BUILDARGS\*(R"\s0, and should return a list
of arguments to pass to the parent class constructor.
.SS "\s-1BUILD\s0"
.IX Subsection "BUILD"
.Vb 5
\& sub BUILD {
\& my ($self, $args) = @_;
\& die "foo and bar cannot be used at the same time"
\& if exists $args\->{foo} && exists $args\->{bar};
\& }
.Ve
.PP
On object creation, any \f(CW\*(C`BUILD\*(C'\fR methods in the class's inheritance hierarchy
will be called on the object and given the results of \*(L"\s-1BUILDARGS\*(R"\s0. They each
will be called in order from the parent classes down to the child, and thus
should not themselves call the parent's method. Typically this is used for
object validation or possibly logging.
.SS "\s-1DEMOLISH\s0"
.IX Subsection "DEMOLISH"
.Vb 4
\& sub DEMOLISH {
\& my ($self, $in_global_destruction) = @_;
\& ...
\& }
.Ve
.PP
When an object is destroyed, any \f(CW\*(C`DEMOLISH\*(C'\fR methods in the inheritance
hierarchy will be called on the object. They are given boolean to inform them
if global destruction is in progress, and are called from the child class upwards
to the parent. This is similar to \*(L"\s-1BUILD\*(R"\s0 methods but in the opposite order.
.PP
Note that this is implemented by a \f(CW\*(C`DESTROY\*(C'\fR method, which is only created on
on the first construction of an object of your class. This saves on overhead for
classes that are never instantiated or those without \f(CW\*(C`DEMOLISH\*(C'\fR methods. If you
try to define your own \f(CW\*(C`DESTROY\*(C'\fR, this will cause undefined results.
.SH "IMPORTED SUBROUTINES"
.IX Header "IMPORTED SUBROUTINES"
.SS "extends"
.IX Subsection "extends"
.Vb 1
\& extends \*(AqParent::Class\*(Aq;
.Ve
.PP
Declares a base class. Multiple superclasses can be passed for multiple
inheritance but please consider using roles instead. The class
will be loaded but no errors will be triggered if the class can't be found and
there are already subs in the class.
.PP
Calling extends more than once will \s-1REPLACE\s0 your superclasses, not add to
them like 'use base' would.
.SS "with"
.IX Subsection "with"
.Vb 1
\& with \*(AqSome::Role1\*(Aq;
.Ve
.PP
or
.PP
.Vb 1
\& with \*(AqSome::Role1\*(Aq, \*(AqSome::Role2\*(Aq;
.Ve
.PP
Composes one or more Moo::Role (or Role::Tiny) roles into the current
class. An error will be raised if these roles cannot be composed because they
have conflicting method definitions. The roles will be loaded using the same
mechanism as \f(CW\*(C`extends\*(C'\fR uses.
.SS "has"
.IX Subsection "has"
.Vb 3
\& has attr => (
\& is => \*(Aqro\*(Aq,
\& );
.Ve
.PP
Declares an attribute for the class.
.PP
.Vb 5
\& package Foo;
\& use Moo;
\& has \*(Aqattr\*(Aq => (
\& is => \*(Aqro\*(Aq
\& );
\&
\& package Bar;
\& use Moo;
\& extends \*(AqFoo\*(Aq;
\& has \*(Aq+attr\*(Aq => (
\& default => sub { "blah" },
\& );
.Ve
.PP
Using the \f(CW\*(C`+\*(C'\fR notation, it's possible to override an attribute.
.PP
.Vb 3
\& has [qw(attr1 attr2 attr3)] => (
\& is => \*(Aqro\*(Aq,
\& );
.Ve
.PP
Using an arrayref with multiple attribute names, it's possible to declare
multiple attributes with the same options.
.PP
The options for \f(CW\*(C`has\*(C'\fR are as follows:
.ie n .IP """is""" 2
.el .IP "\f(CWis\fR" 2
.IX Item "is"
\&\fBrequired\fR, may be \f(CW\*(C`ro\*(C'\fR, \f(CW\*(C`lazy\*(C'\fR, \f(CW\*(C`rwp\*(C'\fR or \f(CW\*(C`rw\*(C'\fR.
.Sp
\&\f(CW\*(C`ro\*(C'\fR stands for \*(L"read-only\*(R" and generates an accessor that dies if you attempt
to write to it \- i.e. a getter only \- by defaulting \f(CW\*(C`reader\*(C'\fR to the name of
the attribute.
.Sp
\&\f(CW\*(C`lazy\*(C'\fR generates a reader like \f(CW\*(C`ro\*(C'\fR, but also sets \f(CW\*(C`lazy\*(C'\fR to 1 and
\&\f(CW\*(C`builder\*(C'\fR to \f(CW\*(C`_build_${attribute_name}\*(C'\fR to allow on-demand generated
attributes. This feature was my attempt to fix my incompetence when
originally designing \f(CW\*(C`lazy_build\*(C'\fR, and is also implemented by
MooseX::AttributeShortcuts. There is, however, nothing to stop you
using \f(CW\*(C`lazy\*(C'\fR and \f(CW\*(C`builder\*(C'\fR yourself with \f(CW\*(C`rwp\*(C'\fR or \f(CW\*(C`rw\*(C'\fR \- it's just that
this isn't generally a good idea so we don't provide a shortcut for it.
.Sp
\&\f(CW\*(C`rwp\*(C'\fR stands for \*(L"read-write protected\*(R" and generates a reader like \f(CW\*(C`ro\*(C'\fR, but
also sets \f(CW\*(C`writer\*(C'\fR to \f(CW\*(C`_set_${attribute_name}\*(C'\fR for attributes that are
designed to be written from inside of the class, but read-only from outside.
This feature comes from MooseX::AttributeShortcuts.
.Sp
\&\f(CW\*(C`rw\*(C'\fR stands for \*(L"read-write\*(R" and generates a normal getter/setter by
defaulting the \f(CW\*(C`accessor\*(C'\fR to the name of the attribute specified.
.ie n .IP """isa""" 2
.el .IP "\f(CWisa\fR" 2
.IX Item "isa"
Takes a coderef which is used to validate the attribute. Unlike Moose, Moo
does not include a basic type system, so instead of doing \f(CW\*(C`isa => \*(AqNum\*(Aq\*(C'\fR,
one should do
.Sp
.Vb 5
\& use Scalar::Util qw(looks_like_number);
\& ...
\& isa => sub {
\& die "$_[0] is not a number!" unless looks_like_number $_[0]
\& },
.Ve
.Sp
Note that the return value for \f(CW\*(C`isa\*(C'\fR is discarded. Only if the sub dies does
type validation fail.
.Sp
Sub::Quote aware
.Sp
Since Moo does \fBnot\fR run the \f(CW\*(C`isa\*(C'\fR check before \f(CW\*(C`coerce\*(C'\fR if a coercion
subroutine has been supplied, \f(CW\*(C`isa\*(C'\fR checks are not structural to your code
and can, if desired, be omitted on non-debug builds (although if this results
in an uncaught bug causing your program to break, the Moo authors guarantee
nothing except that you get to keep both halves).
.Sp
If you want Moose compatible or MooseX::Types style named types, look at
Type::Tiny.
.Sp
To cause your \f(CW\*(C`isa\*(C'\fR entries to be automatically mapped to named
Moose::Meta::TypeConstraint objects (rather than the default behaviour
of creating an anonymous type), set:
.Sp
.Vb 4
\& $Moo::HandleMoose::TYPE_MAP{$isa_coderef} = sub {
\& require MooseX::Types::Something;
\& return MooseX::Types::Something::TypeName();
\& };
.Ve
.Sp
Note that this example is purely illustrative; anything that returns a
Moose::Meta::TypeConstraint object or something similar enough to it to
make Moose happy is fine.
.ie n .IP """coerce""" 2
.el .IP "\f(CWcoerce\fR" 2
.IX Item "coerce"
Takes a coderef which is meant to coerce the attribute. The basic idea is to
do something like the following:
.Sp
.Vb 3
\& coerce => sub {
\& $_[0] % 2 ? $_[0] : $_[0] + 1
\& },
.Ve
.Sp
Note that Moo will always execute your coercion: this is to permit
\&\f(CW\*(C`isa\*(C'\fR entries to be used purely for bug trapping, whereas coercions are
always structural to your code. We do, however, apply any supplied \f(CW\*(C`isa\*(C'\fR
check after the coercion has run to ensure that it returned a valid value.
.Sp
Sub::Quote aware
.Sp
If the \f(CW\*(C`isa\*(C'\fR option is a blessed object providing a \f(CW\*(C`coerce\*(C'\fR or
\&\f(CW\*(C`coercion\*(C'\fR method, then the \f(CW\*(C`coerce\*(C'\fR option may be set to just \f(CW1\fR.
.ie n .IP """handles""" 2
.el .IP "\f(CWhandles\fR" 2
.IX Item "handles"
Takes a string
.Sp
.Vb 1
\& handles => \*(AqRobotRole\*(Aq
.Ve
.Sp
Where \f(CW\*(C`RobotRole\*(C'\fR is a role that defines an interface which
becomes the list of methods to handle.
.Sp
Takes a list of methods
.Sp
.Vb 1
\& handles => [ qw( one two ) ]
.Ve
.Sp
Takes a hashref
.Sp
.Vb 3
\& handles => {
\& un => \*(Aqone\*(Aq,
\& }
.Ve
.ie n .IP """trigger""" 2
.el .IP "\f(CWtrigger\fR" 2
.IX Item "trigger"
Takes a coderef which will get called any time the attribute is set. This
includes the constructor, but not default or built values. The coderef will be
invoked against the object with the new value as an argument.
.Sp
If you set this to just \f(CW1\fR, it generates a trigger which calls the
\&\f(CW\*(C`_trigger_${attr_name}\*(C'\fR method on \f(CW$self\fR. This feature comes from
MooseX::AttributeShortcuts.
.Sp
Note that Moose also passes the old value, if any; this feature is not yet
supported.
.Sp
Sub::Quote aware
.ie n .IP """default""" 2
.el .IP "\f(CWdefault\fR" 2
.IX Item "default"
Takes a coderef which will get called with \f(CW$self\fR as its only argument to
populate an attribute if no value for that attribute was supplied to the
constructor. Alternatively, if the attribute is lazy, \f(CW\*(C`default\*(C'\fR executes when
the attribute is first retrieved if no value has yet been provided.
.Sp
If a simple scalar is provided, it will be inlined as a string. Any non-code
reference (hash, array) will result in an error \- for that case instead use
a code reference that returns the desired value.
.Sp
Note that if your default is fired during \fBnew()\fR there is no guarantee that
other attributes have been populated yet so you should not rely on their
existence.
.Sp
Sub::Quote aware
.ie n .IP """predicate""" 2
.el .IP "\f(CWpredicate\fR" 2
.IX Item "predicate"
Takes a method name which will return true if an attribute has a value.
.Sp
If you set this to just \f(CW1\fR, the predicate is automatically named
\&\f(CW\*(C`has_${attr_name}\*(C'\fR if your attribute's name does not start with an
underscore, or \f(CW\*(C`_has_${attr_name_without_the_underscore}\*(C'\fR if it does.
This feature comes from MooseX::AttributeShortcuts.
.ie n .IP """builder""" 2
.el .IP "\f(CWbuilder\fR" 2
.IX Item "builder"
Takes a method name which will be called to create the attribute \- functions
exactly like default except that instead of calling
.Sp
.Vb 1
\& $default\->($self);
.Ve
.Sp
Moo will call
.Sp
.Vb 1
\& $self\->$builder;
.Ve
.Sp
The following features come from MooseX::AttributeShortcuts:
.Sp
If you set this to just \f(CW1\fR, the builder is automatically named
\&\f(CW\*(C`_build_${attr_name}\*(C'\fR.
.Sp
If you set this to a coderef or code-convertible object, that variable will be
installed under \f(CW\*(C`$class::_build_${attr_name}\*(C'\fR and the builder set to the same
name.
.ie n .IP """clearer""" 2
.el .IP "\f(CWclearer\fR" 2
.IX Item "clearer"
Takes a method name which will clear the attribute.
.Sp
If you set this to just \f(CW1\fR, the clearer is automatically named
\&\f(CW\*(C`clear_${attr_name}\*(C'\fR if your attribute's name does not start with an
underscore, or \f(CW\*(C`_clear_${attr_name_without_the_underscore}\*(C'\fR if it does.
This feature comes from MooseX::AttributeShortcuts.
.Sp
\&\fB\s-1NOTE:\s0\fR If the attribute is \f(CW\*(C`lazy\*(C'\fR, it will be regenerated from \f(CW\*(C`default\*(C'\fR or
\&\f(CW\*(C`builder\*(C'\fR the next time it is accessed. If it is not lazy, it will be \f(CW\*(C`undef\*(C'\fR.
.ie n .IP """lazy""" 2
.el .IP "\f(CWlazy\fR" 2
.IX Item "lazy"
\&\fBBoolean\fR. Set this if you want values for the attribute to be grabbed
lazily. This is usually a good idea if you have a \*(L"builder\*(R" which requires
another attribute to be set.
.ie n .IP """required""" 2
.el .IP "\f(CWrequired\fR" 2
.IX Item "required"
\&\fBBoolean\fR. Set this if the attribute must be passed on object instantiation.
.ie n .IP """reader""" 2
.el .IP "\f(CWreader\fR" 2
.IX Item "reader"
The name of the method that returns the value of the attribute. If you like
Java style methods, you might set this to \f(CW\*(C`get_foo\*(C'\fR
.ie n .IP """writer""" 2
.el .IP "\f(CWwriter\fR" 2
.IX Item "writer"
The value of this attribute will be the name of the method to set the value of
the attribute. If you like Java style methods, you might set this to
\&\f(CW\*(C`set_foo\*(C'\fR.
.ie n .IP """weak_ref""" 2
.el .IP "\f(CWweak_ref\fR" 2
.IX Item "weak_ref"
\&\fBBoolean\fR. Set this if you want the reference that the attribute contains to
be weakened. Use this when circular references, which cause memory leaks, are
possible.
.ie n .IP """init_arg""" 2
.el .IP "\f(CWinit_arg\fR" 2
.IX Item "init_arg"
Takes the name of the key to look for at instantiation time of the object. A
common use of this is to make an underscored attribute have a non-underscored
initialization name. \f(CW\*(C`undef\*(C'\fR means that passing the value in on instantiation
is ignored.
.ie n .IP """moosify""" 2
.el .IP "\f(CWmoosify\fR" 2
.IX Item "moosify"
Takes either a coderef or array of coderefs which is meant to transform the
given attributes specifications if necessary when upgrading to a Moose role or
class. You shouldn't need this by default, but is provided as a means of
possible extensibility.
.SS "before"
.IX Subsection "before"
.Vb 1
\& before foo => sub { ... };
.Ve
.PP
See \*(L"before method(s) => sub { ... };\*(R" in Class::Method::Modifiers for full
documentation.
.SS "around"
.IX Subsection "around"
.Vb 1
\& around foo => sub { ... };
.Ve
.PP
See \*(L"around method(s) => sub { ... };\*(R" in Class::Method::Modifiers for full
documentation.
.SS "after"
.IX Subsection "after"
.Vb 1
\& after foo => sub { ... };
.Ve
.PP
See \*(L"after method(s) => sub { ... };\*(R" in Class::Method::Modifiers for full
documentation.
.SH "SUB QUOTE AWARE"
.IX Header "SUB QUOTE AWARE"
\&\*(L"quote_sub\*(R" in Sub::Quote allows us to create coderefs that are \*(L"inlineable,\*(R"
giving us a handy, XS-free speed boost. Any option that is Sub::Quote
aware can take advantage of this.
.PP
To do this, you can write
.PP
.Vb 1
\& use Sub::Quote;
\&
\& use Moo;
\& use namespace::clean;
\&
\& has foo => (
\& is => \*(Aqro\*(Aq,
\& isa => quote_sub(q{ die "Not <3" unless $_[0] < 3 })
\& );
.Ve
.PP
which will be inlined as
.PP
.Vb 4
\& do {
\& local @_ = ($_[0]\->{foo});
\& die "Not <3" unless $_[0] < 3;
\& }
.Ve
.PP
or to avoid localizing \f(CW@_\fR,
.PP
.Vb 4
\& has foo => (
\& is => \*(Aqro\*(Aq,
\& isa => quote_sub(q{ my ($val) = @_; die "Not <3" unless $val < 3 })
\& );
.Ve
.PP
which will be inlined as
.PP
.Vb 4
\& do {
\& my ($val) = ($_[0]\->{foo});
\& die "Not <3" unless $val < 3;
\& }
.Ve
.PP
See Sub::Quote for more information, including how to pass lexical
captures that will also be compiled into the subroutine.
.SH "CLEANING UP IMPORTS"
.IX Header "CLEANING UP IMPORTS"
Moo will not clean up imported subroutines for you; you will have
to do that manually. The recommended way to do this is to declare your
imports first, then \f(CW\*(C`use Moo\*(C'\fR, then \f(CW\*(C`use namespace::clean\*(C'\fR.
Anything imported before namespace::clean will be scrubbed.
Anything imported or declared after will be still be available.
.PP
.Vb 1
\& package Record;
\&
\& use Digest::MD5 qw(md5_hex);
\&
\& use Moo;
\& use namespace::clean;
\&
\& has name => (is => \*(Aqro\*(Aq, required => 1);
\& has id => (is => \*(Aqlazy\*(Aq);
\& sub _build_id {
\& my ($self) = @_;
\& return md5_hex($self\->name);
\& }
\&
\& 1;
.Ve
.PP
For example if you were to import these subroutines after
namespace::clean like this
.PP
.Vb 1
\& use namespace::clean;
\&
\& use Digest::MD5 qw(md5_hex);
\& use Moo;
.Ve
.PP
then any \f(CW\*(C`Record\*(C'\fR \f(CW$r\fR would have methods such as \f(CW\*(C`$r\->md5_hex()\*(C'\fR,
\&\f(CW\*(C`$r\->has()\*(C'\fR and \f(CW\*(C`$r\->around()\*(C'\fR \- almost certainly not what you
intend!
.PP
Moo::Roles behave slightly differently. Since their methods are
composed into the consuming class, they can do a little more for you
automatically. As long as you declare your imports before calling
\&\f(CW\*(C`use Moo::Role\*(C'\fR, those imports and the ones Moo::Role itself
provides will not be composed into consuming classes so there's usually
no need to use namespace::clean.
.PP
\&\fBOn namespace::autoclean:\fR Older versions of namespace::autoclean would
inflate Moo classes to full Moose classes, losing the benefits of Moo. If
you want to use namespace::autoclean with a Moo class, make sure you are
using version 0.16 or newer.
.SH "INCOMPATIBILITIES WITH MOOSE"
.IX Header "INCOMPATIBILITIES WITH MOOSE"
.SS "\s-1TYPES\s0"
.IX Subsection "TYPES"
There is no built-in type system. \f(CW\*(C`isa\*(C'\fR is verified with a coderef; if you
need complex types, Type::Tiny can provide types, type libraries, and
will work seamlessly with both Moo and Moose. Type::Tiny can be
considered the successor to MooseX::Types and provides a similar \s-1API,\s0 so
that you can write
.PP
.Vb 2
\& use Types::Standard qw(Int);
\& has days_to_live => (is => \*(Aqro\*(Aq, isa => Int);
.Ve
.SS "\s-1API INCOMPATIBILITIES\s0"
.IX Subsection "API INCOMPATIBILITIES"
\&\f(CW\*(C`initializer\*(C'\fR is not supported in core since the author considers it to be a
bad idea and Moose best practices recommend avoiding it. Meanwhile \f(CW\*(C`trigger\*(C'\fR or
\&\f(CW\*(C`coerce\*(C'\fR are more likely to be able to fulfill your needs.
.PP
No support for \f(CW\*(C`super\*(C'\fR, \f(CW\*(C`override\*(C'\fR, \f(CW\*(C`inner\*(C'\fR, or \f(CW\*(C`augment\*(C'\fR \- the author
considers augment to be a bad idea, and override can be translated:
.PP
.Vb 5
\& override foo => sub {
\& ...
\& super();
\& ...
\& };
\&
\& around foo => sub {
\& my ($orig, $self) = (shift, shift);
\& ...
\& $self\->$orig(@_);
\& ...
\& };
.Ve
.PP
The \f(CW\*(C`dump\*(C'\fR method is not provided by default. The author suggests loading
Devel::Dwarn into \f(CW\*(C`main::\*(C'\fR (via \f(CW\*(C`perl \-MDevel::Dwarn ...\*(C'\fR for example) and
using \f(CW\*(C`$obj\->$::Dwarn()\*(C'\fR instead.
.PP
\&\*(L"default\*(R" only supports coderefs and plain scalars, because passing a hash
or array reference as a default is almost always incorrect since the value is
then shared between all objects using that default.
.PP
\&\f(CW\*(C`lazy_build\*(C'\fR is not supported; you are instead encouraged to use the
\&\f(CW\*(C`is => \*(Aqlazy\*(Aq\*(C'\fR option supported by Moo and
MooseX::AttributeShortcuts.
.PP
\&\f(CW\*(C`auto_deref\*(C'\fR is not supported since the author considers it a bad idea and
it has been considered best practice to avoid it for some time.
.PP
\&\f(CW\*(C`documentation\*(C'\fR will show up in a Moose metaclass created from your class
but is otherwise ignored. Then again, Moose ignores it as well, so this
is arguably not an incompatibility.
.PP
Since \f(CW\*(C`coerce\*(C'\fR does not require \f(CW\*(C`isa\*(C'\fR to be defined but Moose does
require it, the metaclass inflation for coerce alone is a trifle insane
and if you attempt to subtype the result will almost certainly break.
.PP
Handling of warnings: when you \f(CW\*(C`use Moo\*(C'\fR we enable strict and warnings, in a
similar way to Moose. The authors recommend the use of \f(CW\*(C`strictures\*(C'\fR, which
enables \s-1FATAL\s0 warnings, and several extra pragmas when used in development:
indirect, multidimensional, and bareword::filehandles.
.PP
Additionally, Moo supports a set of attribute option shortcuts intended to
reduce common boilerplate. The set of shortcuts is the same as in the Moose
module MooseX::AttributeShortcuts as of its version 0.009+. So if you:
.PP
.Vb 3
\& package MyClass;
\& use Moo;
\& use strictures 2;
.Ve
.PP
The nearest Moose invocation would be:
.PP
.Vb 1
\& package MyClass;
\&
\& use Moose;
\& use warnings FATAL => "all";
\& use MooseX::AttributeShortcuts;
.Ve
.PP
or, if you're inheriting from a non-Moose class,
.PP
.Vb 1
\& package MyClass;
\&
\& use Moose;
\& use MooseX::NonMoose;
\& use warnings FATAL => "all";
\& use MooseX::AttributeShortcuts;
.Ve
.SS "\s-1META OBJECT\s0"
.IX Subsection "META OBJECT"
There is no meta object. If you need this level of complexity you need
Moose \- Moo is small because it explicitly does not provide a metaprotocol.
However, if you load Moose, then
.PP
.Vb 1
\& Class::MOP::class_of($moo_class_or_role)
.Ve
.PP
will return an appropriate metaclass pre-populated by Moo.
.SS "\s-1IMMUTABILITY\s0"
.IX Subsection "IMMUTABILITY"
Finally, Moose requires you to call
.PP
.Vb 1
\& _\|_PACKAGE_\|_\->meta\->make_immutable;
.Ve
.PP
at the end of your class to get an inlined (i.e. not horribly slow)
constructor. Moo does it automatically the first time \->new is called
on your class. (\f(CW\*(C`make_immutable\*(C'\fR is a no-op in Moo to ease migration.)
.PP
An extension MooX::late exists to ease translating Moose packages
to Moo by providing a more Moose-like interface.
.SH "COMPATIBILITY WITH OLDER PERL VERSIONS"
.IX Header "COMPATIBILITY WITH OLDER PERL VERSIONS"
Moo is compatible with perl versions back to 5.6. When running on older
versions, additional prerequisites will be required. If you are packaging a
script with its dependencies, such as with App::FatPacker, you will need to
be certain that the extra prerequisites are included.
.IP "MRO::Compat" 4
.IX Item "MRO::Compat"
Required on perl versions prior to 5.10.0.
.IP "Devel::GlobalDestruction" 4
.IX Item "Devel::GlobalDestruction"
Required on perl versions prior to 5.14.0.
.SH "SUPPORT"
.IX Header "SUPPORT"
\&\s-1IRC:\s0 #moose on irc.perl.org
.PP
Bugtracker: <https://rt.cpan.org/Public/Dist/Display.html?Name=Moo>
.PP
Git repository: <git://github.com/moose/Moo.git>
.PP
Git browser: <https://github.com/moose/Moo>
.SH "AUTHOR"
.IX Header "AUTHOR"
mst \- Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
.SH "CONTRIBUTORS"
.IX Header "CONTRIBUTORS"
dg \- David Leadbeater (cpan:DGL) <dgl@dgl.cx>
.PP
frew \- Arthur Axel \*(L"fREW\*(R" Schmidt (cpan:FREW) <frioux@gmail.com>
.PP
hobbs \- Andrew Rodland (cpan:ARODLAND) <arodland@cpan.org>
.PP
jnap \- John Napiorkowski (cpan:JJNAPIORK) <jjn1056@yahoo.com>
.PP
ribasushi \- Peter Rabbitson (cpan:RIBASUSHI) <ribasushi@cpan.org>
.PP
chip \- Chip Salzenberg (cpan:CHIPS) <chip@pobox.com>
.PP
ajgb \- Alex J. G. Burzyński (cpan:AJGB) <ajgb@cpan.org>
.PP
doy \- Jesse Luehrs (cpan:DOY) <doy at tozt dot net>
.PP
perigrin \- Chris Prather (cpan:PERIGRIN) <chris@prather.org>
.PP
Mithaldu \- Christian Walde (cpan:MITHALDU) <walde.christian@googlemail.com>
.PP
ilmari \- Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org>
.PP
tobyink \- Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org>
.PP
haarg \- Graham Knop (cpan:HAARG) <haarg@cpan.org>
.PP
mattp \- Matt Phillips (cpan:MATTP) <mattp@cpan.org>
.PP
bluefeet \- Aran Deltac (cpan:BLUEFEET) <bluefeet@gmail.com>
.PP
bubaflub \- Bob Kuo (cpan:BUBAFLUB) <bubaflub@cpan.org>
.PP
ether = Karen Etheridge (cpan:ETHER) <ether@cpan.org>
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
Copyright (c) 2010\-2015 the Moo \*(L"\s-1AUTHOR\*(R"\s0 and \*(L"\s-1CONTRIBUTORS\*(R"\s0
as listed above.
.SH "LICENSE"
.IX Header "LICENSE"
This library is free software and may be distributed under the same terms
as perl itself. See <https://dev.perl.org/licenses/>.