NAME

Mite::Manual::Syntax - Moose-like syntax supported by Mite

MANUAL

Mite is a subset of Moose. These docs will only describe what Moose features are implemented or where they differ. For everything else, please read Moose and Moose::Manual.

has $name => %spec

Like has in Moose, this declares an attribute. Not all Moose options are supported.

is Enum["ro","rw","bare","rwp","lazy"]

Supported values for is include "ro", "rw", and "bare" like Moose, but also "rwp" and "lazy" like Moo. These are all just shortcuts for defining other options.

The default is is => 'bare'.

reader Maybe[Str]

The name of the attribute reader method to generate. If set to "1", will generate a reader called "get_foo" if your attribute is called "foo" and "_get__foo" if your attribute is called "_foo".

If your attribute is defined as is => "ro", is => "rwp", or is => "lazy" then reader defaults to $name.

writer Maybe[Str]

The name of the attribute writer method to generate. If set to "1", will generate a writer called "set_foo" if your attribute is called "foo" and "_set__foo" if your attribute is called "_foo".

If your attribute is defined as is => "rwp", then writer defaults to "_set_$name".

accessor Maybe[Str]

The name of the dual-purpose attribute reader/writer method to generate. If set to "1", will generate a reader called "fo" if your attribute is called "foo" and "_foo"if your attribute is called "_foo".

If your attribute is defined as is => "rw", then accessor defaults to $name.

clearer Maybe[Str]

The name of the attribute clearer method to generate. If set to "1", will generate a clearer called "clear_foo" if your attribute is called "foo" and "_clear__foo" if your attribute is called "_foo".

predicate Maybe[Str]

The name of the attribute predicate method to generate. If set to "1", will generate a predicate called "has_foo" if your attribute is called "foo" and "_has__foo" if your attribute is called "_foo".

handles HashRef|ArrayRef

Delegated methods. Currying and native traits are not supported.

init_arg Maybe[Str]

The parameter expected to be passed to the constructor to initialize this attribute. May be undef if the attribute should not be intitialized in the constructor.

Defaults to the attribute's name.

required Bool

Indicates whether the attribute is required in the parameter. If the attribute has a non-lazy default, this is ignored.

weak_ref Bool

Indicates that the attribute should weaken its reference to the attribute value.

isa Str|TypeTiny

A string type name from Types::Standard, Types::Common::String, or Types::Common::Numeric, or a blessed Type::Tiny object. If using a blessed object, you'll be introducing a dependency for your project on whatever type constraint library you're using, so prefer string type contraint names.

Any string supported by Type::Utils dwim_type is supported, so things like "Int | Enum['small', 'medium', 'large']" should work!

coerce Bool

Indicates that the attribute should attempt to coerce values to fit the type constraint.

trigger CodeRef|Maybe[Str]

If set to a string, is the name of a method to call whenever a new attribute value is set. This is not called when an attribute is defaulted. If set to "1", will assume a trigger called "_trigger_foo" if your attribute is called "foo" and "_trigger__foo" if your attribute is called "_foo".

If set to a coderef, acts like it was set to "1" but installs the coderef into your class with that name.

default Undef|Str|CodeRef

A default value for the attribute, or a coderef called as a method to generate that default value.

builder Str|CodeRef

If set to a string, is the name of a method to call to build the default attribute value. If set to "1", will assume a builder called "_build_foo" if your attribute is called "foo" and "_build__foo" if your attribute is called "_foo".

If you used is => "lazy", this will default to "1".

If set to a coderef, acts like it was set to "1" but installs the coderef into your class with that name.

lazy Bool

Indicates that the default should be set lazily. Defaults to false unless you used is => "lazy".

alias Str|ArrayRef[Str]

A list of aliases for the attibutes. If the attribute has an init_arg (including a default one), this provides alternative initialization arguments. If the attribute is => "rw", then these aliases are aliases for the accessor; otherwise they are aliases for the reader.

The strings can contain "%s" which will be replaced by the attribute name, allowing this to work:

has [ 'foo', 'bar' ] => (
    is => 'ro',
    alias => 'get_%s',
);

If you try to create aliases but don't have a reader or accessor, then as a last resort the alias will be an alias for the writer.

# foo can be set using `set_foo` or `whatever`
#
has foo => (
    is => 'bare',
    writer => 1,
    alias => 'whatever',
);

Aliases are not natively supported by Moose, but this feature is analagous to MooseX::Aliases.

documentation Any

This option is ignored, but you can set it to a documentation string for your attribute.

has name => (
    is => 'rwp',
    isa => 'Str',
    documentation => 'First name and surname',
);

Multiple attributes can be defined using an arrayref.

# Defines get_foo, get_bar, set_foo, and set_bar.
#
has [ 'foo', 'bar' ] => (
    isa => 'Str',
    reader => 1,
    writer => 1,
);

extends @parents

Works as in Moose.

The -version option is not implemented.

strict

Mite will turn strict on for you.

warnings

Mite will turn warnings on for you.

BUILDARGS

Works as in Moose.

BUILD

Works as in Moose.

DEMOLISH

Works as in Moose.

On Perl older than 5.14, the $in_global_destruction argument will be undefined unless Devel::GlobalDestruction is installed, so add that to your project's dependencies if you are relying on it and need to support older versions of Perl.

Strict Constructor

Mite provides a similar feature to MooseX::StrictConstructor, so your constructor will die if you pass it unknown attributes.

Chainable Writers

Mite provides a similar feature to MooseX::Attribute::Chained, so writer methods, clearer methods, and accessor methods used as writers all return $self, so they can be chained:

$object->clear_foo->set_bar( 42 )->set_baz( 99 );

Method Modifiers

Basic versions of the before, around, and after method modifiers are provided, but these may run in a different order from Moose if you use several modifiers on the same method.

BUGS

Please report any bugs to https://github.com/tobyink/p5-mite/issues.

AUTHOR

Michael G Schwern <mschwern@cpan.org>.

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2011-2014 by Michael G Schwern.

This software is copyright (c) 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.