MooseX::Declare - Declarative syntax for Moose
use MooseX::Declare; class BankAccount { has 'balance' => ( isa => 'Num', is => 'rw', default => 0 ); method deposit (Num $amount) { $self->balance( $self->balance + $amount ); } method withdraw (Num $amount) { my $current_balance = $self->balance(); ( $current_balance >= $amount ) || confess "Account overdrawn"; $self->balance( $current_balance - $amount ); } } class CheckingAccount extends BankAccount { has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' ); before withdraw (Num $amount) { my $overdraft_amount = $amount - $self->balance(); if ( $self->overdraft_account && $overdraft_amount > 0 ) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } } }
This module provides syntactic sugar for Moose, the postmodern object system for Perl 5. When used, it sets up the class and role keywords.
class
role
class Foo { ... } my $anon_class = class { ... };
Declares a new class. The class can be either named or anonymous, depending on whether or not a classname is given. Within the class definition Moose and MooseX::Method::Signatures are set up automatically in addition to the other keywords described in this document. At the end of the definition the class will be made immutable. namespace::clean is injected to clean up Moose for you.
Because of the way the options are parsed, you cannot have a class named "is", "with" or "extends".
It's possible to specify options for classes:
class Foo extends Bar { ... }
Sets a superclass for the class being declared.
class Foo with Role { ... }
Applies a role to the class being declared.
class Foo is mutable { ... }
Causes the class not to be made immutable after its definition.
Options can also be provided for anonymous classes using the same syntax:
my $meta_class = class with Role;
role Foo { ... } my $anon_role = role { ... };
Declares a new role. The role can be either named or anonymous, depending on whether or not a name is given. Within the role definition Moose::Role and MooseX::Method::Signatures are set up automatically in addition to the other keywords described in this document. Again, namespace::clean is injected to clean up Moose::Role and for you.
It's possible to specify options for roles:
role Foo with Bar { ... }
Applies a role to the role being declared.
before foo ($x, $y, $z) { ... } after bar ($x, $y, $z) { ... } around baz ($x, $y, $z) { ... } override moo ($x, $y, $z) { ... } augment kuh ($x, $y, $z) { ... }
Add a method modifier. Those work like documented in Moose, except for the slightly nicer syntax and the method signatures, which work like documented in MooseX::Method::Signatures.
For the around modifier an additional argument called $orig is automatically set up as the invocant for the method.
around
$orig
When creating a class with MooseX::Declare like:
use MooseX::Declare; class Foo { ... }
What actually happens is something like this:
{ package Foo; use Moose; use namespace::clean -except => 'meta'; ... __PACKAGE__->meta->mate_immutable(); 1; }
So if you declare imports outside the class, the symbols get imported into the main:: namespace, not the class' namespace. The symbols then cannot be called from within the class:
main::
use MooseX::Declare; use Data::Dump qw/dump/; class Foo { method dump($value) { return dump($value) } # Data::Dump::dump IS NOT in Foo:: method pp($value) { $self->dump($value) } # an alias for our dump method }
Furthermore, any imports will not be cleaned up by namespace::clean after compilation since the class knows nothing about them! The temptation to do this may stem from wanting to keep all your import declarations in the same place.
The solution is two-fold. First, only import MooseX::Declare outside the class definition (because you have to). Make all other imports inside the class definition and clean up with the clean keyword:
clean
use MooseX::Declare; class Foo { use Data::Dump qw/dump/; clean; method dump($value) { return dump($value) } # Data::Dump::dump IS in Foo:: method pp($value) { $self->dump($value) } # an alias for our dump method } Foo->new->dump($some_value); Foo->new->pp($some_value);
NOTE that the import Data::Dump::dump() and the method Foo::dump(), although having the same name, do not conflict with each other.
Data::Dump::dump()
Foo::dump()
Moose
Moose::Role
MooseX::Method::Signatures
namespace::clean
vim syntax: http://www.vim.org/scripts/script.php?script_id=2526
Florian Ragwitz <rafl@debian.org>
With contributions from:
Copyright (c) 2008, 2009 Florian Ragwitz
Licensed under the same terms as perl itself.
To install MooseX::Declare, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Declare
CPAN shell
perl -MCPAN -e shell install MooseX::Declare
For more information on module installation, please visit the detailed CPAN module installation guide.