NAME
MooseX::Declare - Declarative syntax for Moose
SYNOPSIS
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);
}
}
}
DESCRIPTION
This module provides syntactic sugar for Moose, the postmodern object system for Perl 5. When used, it sets up the class
and role
keywords.
KEYWORDS
class
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.
It's possible to specify options for classes:
- extends
-
class Foo extends Bar { ... }
Sets a superclass for the class being declared.
- with
-
class Foo with Role { ... }
Applies a role to the class being declared.
- is mutable
-
class Foo is mutable { ... }
Causes the class not to be made immutable after its definition.
role
role Foo { ... }
my $anon_role = role { ... };
Declares a new role. The role can be either named or anonymous, depending on wheter 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:
- with
-
role Foo with Bar { ... }
Applies a role to the role being declared.
before / after / around / override / augment
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.
SEE ALSO
AUTHOR
Florian Ragwitz <rafl@debian.org>
COPYRIGHT AND LICENSE
Copyright (c) 2008 Florian Ragwitz
Licensed under the same terms as perl itself.