package MyPackage; use Moo; use MooX::Types::MooseLike::Base qw(:all); has "beers_by_day_of_week" => ( isa => HashRef ); has "current_BAC" => ( isa => Num ); # Also supporting is_$type. For example, is_Int() can be used as follows has 'legal_age' => ( is => 'ro', isa => sub { die "$_[0] is not of legal age" unless (is_Int($_[0]) && $_[0] > 17) }, );
use MooX::Types::MooseLike::Base qw/HashRef ArrayRef/;
so one could then declare some attributes like:
has 'contact' => ( is => 'ro', isa => HashRef, ); has 'guest_list' => ( is => 'ro', isa => ArrayRef[HashRef], );
These types provide a check that the contact attribute is a "hash" reference, and that the guest_list is an "array of hash" references.
ArrayRef
For example, ArrayRef[HashRef]
HashRef
ScalarRef
Maybe
For example, Maybe[Int] would be an integer or undef
Check if the attribute is any of the listed types (think union). Takes a list of types as the argument, for example:
isa => AnyOf[Int, ArrayRef[Int], HashRef[Int]]
Note: AnyOf is passed an ArrayRef[CodeRef]
AllOf
Check if the attribute is all of the listed types (think intersection). Takes a list of types as the argument. For example:
isa => AllOf[ InstanceOf['Human'], ConsumerOf['Air'], HasMethods['breath', 'dance'] ],
InstanceOf
Check if the attribute is an object instance of one or more classes. Uses "blessed" and "isa" to do so. Takes a list of class names as the argument. For example:
isa => InstanceOf['MyClass','MyOtherClass']
Note: InstanceOf is passed an ArrayRef[Str]
ConsumerOf
Check if the attribute is blessed and consumes one or more roles. Uses "blessed" and "does" to do so. Takes a list of role names as the arguments. For example:
isa => ConsumerOf['My::Role', 'My::AnotherRole']
HasMethods
Check if the attribute is blessed and has one or more methods. Uses "blessed" and "can" to do so. Takes a list of method names as the arguments. For example:
isa => HasMethods[qw/postulate contemplate liberate/]
Enum
Check if the attribute is one of the enumerated strings. Takes a list of possible string values. For example:
isa => Enum['rock', 'spock', 'paper', 'lizard', 'scissors']
MooX::Types::SetObject - an example of building parameterized types.
MooX::Types::MooseLike::Email, MooX::Types::MooseLike::DateTime