NAME
MooX::Aspartame - it seems sweet, but it probably has long-term adverse health effects
SYNOPSIS
use MooX::Aspartame;
role NamedThing {
has name => (is => "ro", isa => Str);
}
class Person with NamedThing;
class Company with NamedThing;
class Employee extends Person {
has job_title => (is => "rwp", isa => Str);
has employer => (is => "rwp", isa => InstanceOf["Company"]);
method change_job ( Object $employer, Str $title ) {
$self->_set_job_title($title);
$self->_set_employer($employer);
}
method promote ( Str $title ) {
$self->_set_job_title($title);
}
}
DESCRIPTION
This is something like a lightweight MooseX::Declare. (Only 40% as many dependencies; and loads in about 25% of the time.)
It gives you three keywords:
class
-
Declares a class. By default this uses Moo. But it's possible to promote a class to Moose with the
using
option:class Employee using Moose { ... }
Other options for classes are
extends
for setting a parent class, andwith
for composing roles.class Employee extends Person with Employment;
Note that if you're not directly defining any methods for a class, you can use a trailing semicolon (as above) rather than an empty
{ }
pair. role
-
Declares a role using Moo::Role. This also supports
using Moose
, andwith
. namespace
-
Declares a package without giving it any special semantics.
Note that the names of the declared things get qualified like subs. So:
package Foo;
use MooX::Aspartame;
class Bar { # declares Foo::Bar
role Baz { # declares Foo::Bar::Baz
...;
}
class Xyzzy with Baz;
}
class ::Quux { # declares Quux
...;
}
package main;
use MooX::Aspartame;
class Bar { # declares Bar
...;
}
Within the packages declared by these keywords, the following features are always available:
Perl 5.14 features. (MooX::Aspartame requires Perl 5.14.)
Strictures, including
FATAL
warnings.But not
uninitialized
,void
,once
ornumeric
warnings, because those are irritating.Function::Parameters (in strict mode).
This provides the
fun
keyword.Within roles and classes, it also provides
method
, and thebefore
,after
andaround
method modifiers. Unlike Moo/Moose, withinaround
modifiers the coderef being wrapped is not available in$_[0]
, but is instead found in the magic global variable${^NEXT}
.A
define
keyword to declare constants:use MooX::Aspartame; class Calculator { define PI = 3.2; method circular_area (Num $r) { return PI * ($r ** 2); } } my $calc = Calculator->new; say "The circle's area is ", $calc->circular_area(r => 1.0);
Types::Standard type constraints
Carp's
confess
Scalar::Util's
blessed
Constants for
true
andfalse
.namespace::sweep (only for classes and roles).
It is possible to inject other functions into all packages using:
use MooX::Aspartame [
'List::Util' => [qw( first reduce )],
'List::MoreUtils' => [qw( any all none )],
];
In the "outer" package (where MooX::Aspartame is used), strictures and true are provided.
BUGS
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=MooX-Aspartame.
SEE ALSO
Similar: MooseX::Declare, https://github.com/stevan/p5-mop-redux.
Main functionality exposed by this module: Moo/MooX::late, Function::Parameters, Try::Tiny, Types::Standard, namespace::sweep, true.
Internals fueled by: Keyword::Simple, Module::Runtime, Import::Into, Devel::Pragma, Attribute::Handlers.
AUTHOR
Toby Inkster <tobyink@cpan.org>.
COPYRIGHT AND LICENCE
This software is copyright (c) 2013 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.
DISCLAIMER OF WARRANTIES
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.