MooseX::Extended::Manual::Shortcuts - Shortcuts to make your Moose easier to write
version 0.02
When using field or param, we have some attribute shortcuts:
field
param
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; use MooseX::Extended::Types qw(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.
1
set_$attribute_name
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.
get_$attribute_name
param title => ( isa => Undef | NonEmptyStr, default => undef, reader => 1, );
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.
has_$attribute_name
param title => ( isa => Undef | NonEmptyStr, default => undef, predicate => 1, );
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.
clear_$attribute_name
param title => ( isa => Undef | NonEmptyStr, default => undef, clearer => 1, );
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.
_build_$attribute_name
param title => ( isa => NonEmptyStr, builder => 1, );
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").
_auth_token
_build__auth_token
Curtis "Ovid" Poe <curtis.poe@gmail.com>
This software is Copyright (c) 2022 by Curtis "Ovid" Poe.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install MooseX::Extended, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Extended
CPAN shell
perl -MCPAN -e shell install MooseX::Extended
For more information on module installation, please visit the detailed CPAN module installation guide.