NAME
MooseX::Extended::Manual::Shortcuts - Shortcuts to make your Moose easier to write
VERSION
version 0.23
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',
);
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").
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)