Mom - Moo objects minimally
This:
use Mom;
Is (roughly) a shortcut for:
use Moo; use Scalar::Util qw( blessed ); use Carp qw( carp croak confess ); use namespace::autoclean;
But Mom takes care of a lot more. This:
use Mom q{ foo bar :rw :type(Int) baz :required };
use Moo; use Scalar::Util qw( blessed ); use Carp qw( carp croak confess ); use Types::Standard qw(); use namespace::autoclean; has foo => ( is => "ro" ); has bar => ( is => "rw", isa => Types::Standard::Int ); has baz => ( is => "ro", required => 1 );
Tokens which don't start with a colon are created as attributes in your package. Tokens starting with a colon are flags that affect either the preceeding attribute or the package as a whole.
This documentation assumes familiarity with Moo.
The documentation for MooX::ShortHas says instead of this:
use Moo; has hro => is => ro => required => 1; has hlazy => is => lazy => builder => sub { 2 }; has hrwp => is => rwp => required => 1; has hrw => is => rw => required => 1;
You can now write this:
use Moo; use MooX::ShortHas; ro "hro"; lazy hlazy => sub { 2 }; rwp "hrwp"; rw "hrw";
I thought I could go even shorter.
use Mom q{ hro :required hlazy :lazy :default(2) hrwp :required :rwp hrw :required :rw };
Is it a lot shorter? No, but that's mostly because MooX::ShortHas makes it very concise to write required attributes, and uses that in the demonstration but less concise for optional attributes. If we make the attributes optional, it becomes shorter.
use Mom q{ hro hlazy :lazy :default(2) hrwp :rwp hrw :rw };
All of Mom's magic happens in the import statement.
:rw
Like is => "rw" in Moo.
is => "rw"
:ro
Like is => "ro" in Moo, though this is already the default.
is => "ro"
:rwp
Like is => "rwp" in Moo
is => "rwp"
:bare
Like is => "bare" in Moo
is => "bare"
:lazy
Like lazy => 1 in Moo.
lazy => 1
:required
Like required => 1 in Moo.
required => 1
:clearer
Like clearer => 1 in Moo.
clearer => 1
:clearer(methodname)
Like clearer => "methodname" in Moo.
clearer => "methodname"
:builder
Like builder => 1 in Moo.
builder => 1
:builder(methodname)
Like builder => "methodname" in Moo.
builder => "methodname"
:trigger
Like trigger => 1 in Moo.
trigger => 1
:trigger(methodname)
Like trigger => "methodname" in Moo.
trigger => "methodname"
:isa(Class::Name)
Like isa => InstanceOf[Class::Name] in Moo/Types::Standard.
isa => InstanceOf[Class::Name]
:does(Role::Name)
Like isa => ConsumerOf[Role::Name] in Moo/Types::Standard.
isa => ConsumerOf[Role::Name]
:type(TypeName)
Like isa => TypeName in Moo/Types::Standard.
isa => TypeName
:enum(list,of,strings)
Like isa => Enum["list","of","strings"] in Moo/Types::Standard.
isa => Enum["list","of","strings"]
:default(value)
Like default => "value" in Moo.
default => "value"
For simple (string/numeric) defaults. Doesn't accept coderefs.
:handles(list,of,methods)
Like handles => ["list","of","methods"] in Moo.
handles => ["list","of","methods"]
Currently no support for a hashref of delegations.
:role
Creates a Moo::Role instead of a Moo class.
:extends(Class::Name)
Like extends "Class::Name" in Moo.
extends "Class::Name"
:with(Role::Name)
Like with "Role::Name" in Moo.
with "Role::Name"
:requires(list,of,methods)
Like requires ("list", "of", "methods"); in Moo::Role.
requires ("list", "of", "methods");
:std
Like use Types::Standard qw( -types -is -assert )
use Types::Standard qw( -types -is -assert )
:common
Like:
use Types::Common::Numeric qw( -types -is -assert ); use Types::Common::String qw( -types -is -assert );
:path
use Types::Path::Tiny qw( -types -is -assert ); use Path::Tiny qw( path );
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Mom.
Moo, Types::Standard.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2020 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.
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.
To install Mom, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mom
CPAN shell
perl -MCPAN -e shell install Mom
For more information on module installation, please visit the detailed CPAN module installation guide.