NAME
Mew - Moo with sugar on top
SYNOPSIS
#
# This:
#
use Mew;
has _foo => PositiveNum;
has -_bar => Bool; # note the minus: it means attribute is not `required`
has type => ( Str, default => 'html', chained => 1); # fluent interface
has _cust => ( is => 'ro', isa => sub{ 42 } ); # standard Moo `has`
has [qw/_ar1 -_ar2/] => Str; # Multiple args
#
# Is the same as:
#
use strictures 2;
use Types::Standard qw/:all/;
use Types::Common::Numeric qw/:all/;
use Moo;
use MooX::ChainedAttributes;
use namespace::clean;
has _foo => (
init_arg => 'foo',
is => 'ro'
isa => PositiveNum,
required => 1,
);
has _bar => (
init_arg => 'bar',
is => 'ro'
isa => Bool,
);
has type => (
chained => 1,
is => 'rw'
isa => Str,
default => 'html',
);
has _cust => (
is => 'ro',
isa => sub{ 42 },
);
has _ar1 => (
init_arg => 'ar1',
is => 'ro'
isa => Str,
required => 1,
);
has ar2 => (
init_arg => 'ar2',
is => 'ro'
isa => Str,
);
DESCRIPTION
This module is just like regular Moo, except it also imports strictures and namespace::clean, along with a couple of standard types modules. In addition, it sweetens the Moo's has subroutine to allow for more concise attribute declarations.
READ FIRST
Virtually all of the functionality is described in Moo.
IMPORTED MODULES
use Mew;
Automatically imports the following modules: Moo, strictures, Types::Standard, Types::Common::Numeric, MooX::ChainedAttributes, and namespace::clean. NOTE: in particular the last one. It'll scrub your namespace, thus if you're using things like experimental, you should declare them after you use Mew
.
has
SUGAR
Call it like if it were Moo
has _cust => ( is => 'ro' );
First, you can call has
just like you'd call "has" in Moo and it'll work exactly as it used to. The sugar won't be enabled in that case.
Specify isa
type to get sugar
has _cust => Str;
has _cust => ( Str, default => "foo" ); # Note: can't use "=>" after Str
has [qw/_z1 -z2/] => Str;
To get the sugar, you need to specify one of the imported types from either Types::Standard or Types::Common::Numeric as the second argument. Once that is done, Mew
will add some default settings, which are:
1) Set `isa` to the type you gave
2) Set `is` to 'ro' (or 'rw', if `chained` is set)
3) Set `require` to 1
4) Set `init_arg` to the name of the attribute, removing
the leading underscore, if it's present
Thus, has _cust => Str;
is equivalent to
use Types::Standard qw/Str/;
has _cust => (
init_arg => 'cust',
is => 'ro'
isa => Str,
required => 1,
);
You can specify same settings for multiple attributes by providing their names in an arrayref:
has [qw/_z1 -z2/] => Str;
IMPORTANT NOTE: because Perl's fat comma (=>
) quotes the argument on the left side, using it after the type won't work:
has _cust => Str => ( default => "BROKEN" ); # WRONG!!!!
has _cust => Str, ( default => "WORKS" ); # Correct!
has _cust => ( Str, default => "WORKS" ); # This is fine too
Method chaining
package Foo;
use Mew;
has cost => ( PostiveNum, chained => 1 );
has weight => ( PostiveNum, chained => 1 );
has size => ( Str, chained => 1 );
...
my $object = Foo->new->cost( 42 )->weight( 45 )->size("X-Large");
say $object->size; # prints "X-Large"
To have fluent interface or allow "chaining" your attributes, simply add chained => 1
option to your attribute declaration. Note: this will automatically use rw
instead of ro
for the default of the is
option.
Modify the sugar
It's possible to alter the defaults created by Mew
:
Remove required
has -_cust => Str;
Simply prefix the attribute's name with a minus sign to avoid setting required => 1
.
Alternatively, use the Optional
type provided by Types::Standard.
has _cust => Optional[Str];
Modify other options
has _cust => Str, ( init arg => "bar" );
has -_cust => Str, ( is => "lazy" );
You can explicitly provide values for options set by Mew
, in which case the values you provide will be used instead of the defaults.
SEE ALSO
REPOSITORY
Fork this module on GitHub: https://github.com/zoffixznet/Mew
BUGS
To report bugs or request features, please use https://github.com/zoffixznet/Mew/issues
If you can't access GitHub, you can email your request to bug-Mew at rt.cpan.org
AUTHOR
Part of the code was borrowed from Moo's innards. ew module is an almost-verbatim copy of oo module. Thanks to Matt S. Trout (mst) for changing my copypasta of Moo's internals to sane code and other help. Props to Altreus for coming up with the name for the module.
The rest is:
CONTRIBUTORS
LICENSE
You can use and distribute this module under the same terms as Perl itself. See the LICENSE
file included in this distribution for complete details.