Attribute Constructor Options Moose Quick-Ref Card trigger => sub {…} A modern object system for 5 is => ‘rw’|’ro’ Code to run after attribute is set. Is Creates a read/write or read-only passed ($self, $new_val). accessor. If you omit this option, no Exported Functions accessor will be created. default => $val | sub{ []|{}|sub{…} } use Moose; isa => $type_name | '$ta|$tb|…' Default value to initialize attribute. Turns on strict and warnings. Set up run-time type checking. The outer sub{} is passed $self. Exports confess and blessed. See below for $type_name details. predicate => $name extends @superclasses does => $role Method $name will perform a basic Moose’s alternative to use base. Value’s class must consume $role. defined test on the attribute. Note that it will re-set @ISA. => $name reader|writer|clearer => $sub_name with @roles Extend attribute via a metaclass. Provide the subroutine names used to with $role => { %options } read from, write to, and uninitialize the Consume roles (interfaces) as an traits => [@role_names] stored value. alternative to extending classes. Apply roles to attribute’s meta-object. builder => $sub_name has $name => %options coerce => 1|0 Separate method to return default value. Allow coercion to $type_name on storage. Install an attribute into this class. Better than default for subclassing. See below for details. See below for %options details. lazy_build => 1 required => 1|0 has “+$name” => %options Sets lazy, required, predicate (has_$name), Attribute must always have a value. Clone and extend an attribute. clearer (clear_$name) and

builder (_build_$name). before @names => sub {…} weak_ref => 1|0 around @names => sub {…} Value is stored as weakened ref init_arg => $name after @names => sub {…} (note: conflicts with coercion). Name for attribute when passed into the Extend a superclass’s method. around is constructor, or disallowed if undef. passed ($next_method, $self, @args). lazy => 1|0 Don’t create a value from the (required) override $name => sub { super() } handles => default until accessed. @ary|%hsh|qr//|$role|sub{…} Explicit override of a method. Sets up methods which delegate to

auto_deref => 1|0 methods of the value’s class. augment $name => sub { inner() } Accessor will dereference array or hash The inverse of override/super. Requires that isa be set. references (isa must be set).

Data Type Constraints Data Type Constraints, continued… Other Tidbits

The built-in type-constraints are: enum $name => @values; use Moose::Role; Constraining to a list of Str values. A role (or interface or trait) can only be Any Maybe[TypeName] consumed, not instantiated directly. Item subtype 'TypeName' Bool => as class_type'SomeClass'; requires @methods; Undef (use with care) Idiomatic check of value’s class. Defined Methods which must be implemented by Value the consuming class. Num has $name => (isa => 'SomeClass'); Int Magical version of above. my $meta = __PACKAGE__->meta; Str RoleName Get the cached metaclass for a package. ClassName (means “is loaded” and isa) Ref Data Type Coercions $meta->make_immutable; ScalarRef no Moose; ArrayRef or ArrayRef[TypeName] no Moose::Role; HashRef or HashRef[TypeName] use Moose::Util::TypeConstraints; CodeRef coerce $type Finalize the class to make it faster, RegexpRef => from $some_type and unimport the Moose ‘keywords’. GlobRef => via { } FileHandle => from $some_other_type Object The BUILD method of each class will be Role => via { }; executed after the type constraint checks Instruct Moose in how to coerce data by the constructor, and is passed To define your own, global types: from $some_type to $type. You can chain ($self, $params). use Moose::Util::TypeConstraints; alternative coercions as shown. type $name Before that, BUILDARGS is passed Coercion is passed a value in $_ and => where { } ($class, @params) to convert into the returns the value to be stored. => message { $message }; $params hashref.

A new type-constraint with no parent. Choice Related Modules The DEMOLISH method of each class is subtype $name called at object destruction. => as $parent Class::MOP => where { } • => message { $message }; • Moose::Exporter Meta Class and Trait namespaces: Subtype of an existing type. • MooseX::AttributeHelpers Moose::Meta::Attribute::Custom::$metaclass MooseX::ClassAttribute Moose::Meta::$type::Custom::Trait::$trait • It is recommended that you always quote • MooseX::Getopt $name. Moose checks $parent constraints • MooseX::Object::Pluggable This quick-ref card is © Oliver Gorwits first. The block of must evaluate to • MooseX::Role::Parameterized 2012-06-06 version 4.2 true. A $message is optional, and used in • MooseX::Storage http://get.moosequickref.pl confess if the constraint check fails. Thanks to many people from #moose • MooseX::Types