1MooseX::Declare(3) User Contributed Perl Documentation MooseX::Declare(3)
2
3
4
6 MooseX::Declare - Declarative syntax for Moose
7
9 use MooseX::Declare;
10
11 class BankAccount {
12 has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );
13
14 method deposit (Num $amount) {
15 $self->balance( $self->balance + $amount );
16 }
17
18 method withdraw (Num $amount) {
19 my $current_balance = $self->balance();
20 ( $current_balance >= $amount )
21 || confess "Account overdrawn";
22 $self->balance( $current_balance - $amount );
23 }
24 }
25
26 class CheckingAccount extends BankAccount {
27 has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
28
29 before withdraw (Num $amount) {
30 my $overdraft_amount = $amount - $self->balance();
31 if ( $self->overdraft_account && $overdraft_amount > 0 ) {
32 $self->overdraft_account->withdraw($overdraft_amount);
33 $self->deposit($overdraft_amount);
34 }
35 }
36 }
37
39 This module provides syntactic sugar for Moose, the postmodern object
40 system for Perl 5. When used, it sets up the "class" and "role"
41 keywords.
42
44 class
45 class Foo { ... }
46
47 my $anon_class = class { ... };
48
49 Declares a new class. The class can be either named or anonymous,
50 depending on whether or not a classname is given. Within the class
51 definition Moose and MooseX::Method::Signatures are set up
52 automatically in addition to the other keywords described in this
53 document. At the end of the definition the class will be made
54 immutable. namespace::autoclean is injected to clean up Moose and other
55 imports for you.
56
57 Because of the way the options are parsed, you cannot have a class
58 named "is", "with" or "extends".
59
60 It's possible to specify options for classes:
61
62 extends
63 class Foo extends Bar { ... }
64
65 Sets a superclass for the class being declared.
66
67 with
68 class Foo with Role { ... }
69 class Foo with Role1 with Role2 { ... }
70 class Foo with (Role1, Role2) { ... }
71
72 Applies a role or roles to the class being declared.
73
74 is mutable
75 class Foo is mutable { ... }
76
77 Causes the class not to be made immutable after its definition.
78
79 Options can also be provided for anonymous classes using the same
80 syntax:
81
82 my $meta_class = class with Role;
83
84 role
85 role Foo { ... }
86
87 my $anon_role = role { ... };
88
89 Declares a new role. The role can be either named or anonymous,
90 depending on whether or not a name is given. Within the role definition
91 Moose::Role and MooseX::Method::Signatures are set up automatically in
92 addition to the other keywords described in this document. Again,
93 namespace::autoclean is injected to clean up Moose::Role and other
94 imports for you.
95
96 It's possible to specify options for roles:
97
98 with
99 role Foo with Bar { ... }
100
101 Applies a role to the role being declared.
102
103 before / after / around / override / augment
104 before foo ($x, $y, $z) { ... }
105 after bar ($x, $y, $z) { ... }
106 around baz ($x, $y, $z) { ... }
107 override moo ($x, $y, $z) { ... }
108 augment kuh ($x, $y, $z) { ... }
109
110 Add a method modifier. Those work like documented in Moose, except for
111 the slightly nicer syntax and the method signatures, which work like
112 documented in MooseX::Method::Signatures.
113
114 For the "around" modifier an additional argument called $orig is
115 automatically set up as the invocant for the method.
116
117 clean
118 Sometimes you don't want the automatic cleaning the "class" and "role"
119 keywords provide using namespace::autoclean. In those cases you can
120 specify the "dirty" trait for your class or role:
121
122 use MooseX::Declare;
123 class Foo is dirty { ... }
124
125 This will prevent cleaning of your namespace, except for the keywords
126 imported from "Moose" or "Moose::Role". Additionally, a "clean" keyword
127 is provided, which allows you to explicitly clean all functions that
128 were defined prior to calling "clean". Here's an example:
129
130 use MooseX::Declare;
131 class Foo is dirty {
132 sub helper_function { ... }
133 clean;
134 method foo ($stuff) { ...; return helper_function($stuff); }
135 }
136
137 With that, the helper function won't be available as a method to a user
138 of your class, but you're still able to use it inside your class.
139
141 When creating a class with MooseX::Declare like:
142
143 use MooseX::Declare;
144 class Foo { ... }
145
146 What actually happens is something like this:
147
148 {
149 package Foo;
150 use Moose;
151 use namespace::autoclean;
152 ...
153 __PACKAGE__->meta->make_immutable;
154 }
155
156 So if you declare imports outside the class, the symbols get imported
157 into the "main::" namespace, not the class' namespace. The symbols then
158 cannot be called from within the class:
159
160 use MooseX::Declare;
161 use Data::Dump qw/dump/;
162 class Foo {
163 method dump($value) { return dump($value) } # Data::Dump::dump IS NOT in Foo::
164 method pp($value) { $self->dump($value) } # an alias for our dump method
165 }
166
167 To solve this, only import MooseX::Declare outside the class definition
168 (because you have to). Make all other imports inside the class
169 definition.
170
171 use MooseX::Declare;
172 class Foo {
173 use Data::Dump qw/dump/;
174 method dump($value) { return dump($value) } # Data::Dump::dump IS in Foo::
175 method pp($value) { $self->dump($value) } # an alias for our dump method
176 }
177
178 Foo->new->dump($some_value);
179 Foo->new->pp($some_value);
180
181 NOTE that the import "Data::Dump::dump()" and the method "Foo::dump()",
182 although having the same name, do not conflict with each other, because
183 the imported "dump" function will be cleaned during compile time, so
184 only the method remains there at run time. If you want to do more
185 esoteric things with imports, have a look at the "clean" keyword and
186 the "dirty" trait.
187
189 Moose
190
191 Moose::Role
192
193 MooseX::Method::Signatures
194
195 namespace::autoclean
196
197 vim syntax: <http://www.vim.org/scripts/script.php?script_id=2526>
198
199 emacs syntax: http://github.com/jrockway/cperl-mode
200 <http://github.com/jrockway/cperl-mode>
201
202 Geany syntax + notes:
203 http://www.cattlegrid.info/blog/2009/09/moosex-declare-geany-syntax.html
204 <http://www.cattlegrid.info/blog/2009/09/moosex-declare-geany-
205 syntax.html>
206
208 Florian Ragwitz <rafl@debian.org>
209
210 With contributions from:
211
212 Ash Berlin <ash@cpan.org>
213 Chas. J. Owens IV <chas.owens@gmail.com>
214 Chris Prather <chris@prather.org>
215 Dave Rolsky <autarch@urth.org>
216 Devin Austin <dhoss@cpan.org>
217 Hans Dieter Pearcey <hdp@cpan.org>
218 Justin Hunter <justin.d.hunter@gmail.com>
219 Matt Kraai <kraai@ftbfs.org>
220 Michele Beltrame <arthas@cpan.org>
221 Nelo Onyiah <nelo.onyiah@gmail.com>
222 nperez <nperez@cpan.org>
223 Piers Cawley <pdcawley@bofh.org.uk>
224 Rafael Kitover <rkitover@io.com>
225 Robert aXXphaylonaXX Sedlacek <rs@474.at>
226 Stevan Little <stevan.little@iinteractive.com>
227 Tomas Doran <bobtfish@bobtfish.net>
228 Yanick Champoux <yanick@babyl.dyndns.org>
229
231 Copyright (c) 2008, 2009 Florian Ragwitz
232
233 Licensed under the same terms as perl itself.
234
235
236
237perl v5.12.1 2010-02-04 MooseX::Declare(3)