NAME

MooseX::Extended::Manual::Shortcuts - Shortcuts to make your Moose easier to write

VERSION

version 0.26

ATTRIBUTE SHORTCUTS

When using field or param, we have some attribute shortcuts:

param name => (
    isa       => NonEmptyStr,
    writer    => 1,   # set_name
    reader    => 1,   # get_name
    predicate => 1,   # has_name
    clearer   => 1,   # clear_name
    builder   => 1,   # _build_name
);

sub _build_name ($self) {
    ...
}

These can also be used when you pass an array reference to the function:

package Point {
    use MooseX::Extended types => ['Int'];

    param [ 'x', 'y' ] => (
        isa     => Int,
        clearer => 1,     # clear_x and clear_y available
        default => 0,
    );
}

Note that these are shortcuts and they make attributes easier to write and more consistent. However, you can still use full names:

field authz_delegate => (
    builder => '_build_my_darned_authz_delegate',
);

These are very similar to MooseX::AttributeShortcuts, but the naming is slightly different. For example, clearer => 1 for a _name attribute creates a clear__name method. but for MooseX::AttributeShortcuts, it would have been named _clear_name.

You still have the has function available for defining attributes, but it is unchanged. These shortcuts will not work.

writer

If an attribute has writer is set to 1 (the number one), a method named set_$attribute_name is created.

This:

param title => (
    isa       => Undef | NonEmptyStr,
    default   => undef,
    writer => 1,
);

Is the same as this:

has title => (
    is      => 'rw',                  # we change this from 'ro'
    isa     => Undef | NonEmptyStr,
    default => undef,
    writer  => 'set_title',
);

reader

By default, the reader (accessor) for the attribute is the same as the name. You can always change this:

has payload => ( is => 'ro', reader => 'the_payload' );

However, if you want to change the reader name

If an attribute has reader is set to 1 (the number one), a method named get_$attribute_name is created.

This:

param title => (
    isa       => Undef | NonEmptyStr,
    default   => undef,
    reader => 1,
);

Is the same as this:

has title => (
    is      => 'rw',                  # we change this from 'ro'
    isa     => Undef | NonEmptyStr,
    default => undef,
    reader  => 'get_title',
);

predicate

If an attribute has predicate is set to 1 (the number one), a method named has_$attribute_name is created.

This:

param title => (
    isa       => Undef | NonEmptyStr,
    default   => undef,
    predicate => 1,
);

Is the same as this:

has title => (
    is        => 'ro',
    isa       => Undef | NonEmptyStr,
    default   => undef,
    predicate => 'has_title',
);

clearer

If an attribute has clearer is set to 1 (the number one), a method named clear_$attribute_name is created.

This:

param title => (
    isa     => Undef | NonEmptyStr,
    default => undef,
    clearer => 1,
);

Is the same as this:

has title => (
    is      => 'ro',
    isa     => Undef | NonEmptyStr,
    default => undef,
    clearer => 'clear_title',
);

builder

If an attribute has builder is set to 1 (the number one), a method named _build_$attribute_name.

This:

param title => (
    isa     =>  NonEmptyStr,
    builder => 1,
);

Is the same as this:

has title => (
    is      => 'ro',
    isa     => NonEmptyStr,
    builder => '_build_title',
);

Obviously, a "private" attribute, such as _auth_token would get a build named _build__auth_token (note the two underscores between "build" and "auth_token").

STEALING FROM MOO

is => 'rwp'

param name => ( is => 'rwp' );

The above is equivalent to:

has name => ( is => 'ro', writer => '_set_name' );

Of course, it works for field, too.

builder Code References

You may also pass a coderef to `builder`:

field created => (
    isa     => PositiveInt,
    lazy    => 0,
    builder => sub {time},
);

This is different from default => sub {...} because it will install a _build_created method for you. This is useful if you with to allow a subclass to override this method.

DIAGNOSTICS

Attributes defined using param or field which are read-only with no init_arg and no default or builder, will result in a warning. If you wish to disable this warning you can.

no warnings 'MooseX::Extended::naked_fields';

This warning is only available on Perl >= 5.028.

AUTHOR

Curtis "Ovid" Poe <curtis.poe@gmail.com>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2022 by Curtis "Ovid" Poe.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)