The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MooX::Types::MooseLike::Base - Moose like types for Moo

SYNOPSIS

    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) },
    );

DESCRIPTION

Moo attributes (like Moose) have an 'isa' property. This module provides some basic types for this property. One can import all types with ':all' tag or import a list of types like:

    use MooX::Types::MooseLike::Base qw/HashRef CodeRef/;

so one could then declare some attributtes like:

        has 'contact' => (
          is => 'ro',
          isa => HashRef,
        );
        has 'guest_list' => (
          is => 'ro',
          isa => ArrayRef,
        );
        has 'records' => (
          is => 'ro',
          isa => ArrayRef[Int],
        );

These types provide a check that the contact attribute is a hash reference, that the guest_list is an array reference, and that the records are an array of hash references.

TYPES (subroutines)

Any

Any type (test is always true)

Item

Synonymous with Any type

Undef

A type that is not defined

Defined

A type that is defined

Bool

A boolean 1|0 type

Value

A non-reference type

Ref

A reference type

Str

A non-reference type where a reference to it is a SCALAR

Num

A number type

Int

An integer type

ArrayRef

An ArrayRef (ARRAY) type

HashRef

A HashRef (HASH) type

CodeRef

A CodeRef (CODE) type

RegexpRef

A regular expression reference type

GlobRef

A glob reference type

FileHandle

A type that is either a builtin perl filehandle or an IO::Handle object

Object

A type that is an object (think blessed)

PARAMETERIZED TYPES

Parameterizing Types With Other Types

The following types can be parameterized with other types.

ArrayRef

For example, ArrayRef[HashRef]

HashRef

ScalarRef

Maybe

For example, Maybe[Int] would be an integer or undef

Parameterizing Types With Strings

In addition, we have some parameterized types that take string arguments.

InstanceOf

Takes a class name as the argument. For example:

  isa => InstanceOf['MyClass']

ConsumerOf

Takes a list of role names as the arguments. For example:

  isa => ConsumerOf['My::Role', 'My::AnotherRole'] 

HasMethods

Takes a list of method names as the arguments. For example:

  isa => HasMethods[qw/postulate contemplate liberate/]

AUTHOR

Mateu Hunter hunter@missoula.org

THANKS

mst has provided critical guidance on the design

COPYRIGHT

Copyright 2011, 2012 Mateu Hunter

LICENSE

You may distribute this code under the same terms as Perl itself.