1MooseX::Declare(3pm)  User Contributed Perl Documentation MooseX::Declare(3pm)
2
3
4

NAME

6       MooseX::Declare - (DEPRECATED) Declarative syntax for Moose
7

VERSION

9       version 0.43
10

SYNOPSIS

12           use MooseX::Declare;
13
14           class BankAccount {
15               has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );
16
17               method deposit (Num $amount) {
18                   $self->balance( $self->balance + $amount );
19               }
20
21               method withdraw (Num $amount) {
22                   my $current_balance = $self->balance();
23                   ( $current_balance >= $amount )
24                       || confess "Account overdrawn";
25                   $self->balance( $current_balance - $amount );
26               }
27           }
28
29           class CheckingAccount extends BankAccount {
30               has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
31
32               before withdraw (Num $amount) {
33                   my $overdraft_amount = $amount - $self->balance();
34                   if ( $self->overdraft_account && $overdraft_amount > 0 ) {
35                       $self->overdraft_account->withdraw($overdraft_amount);
36                       $self->deposit($overdraft_amount);
37                   }
38               }
39           }
40

DESCRIPTION

42       This module provides syntactic sugar for Moose, the postmodern object
43       system for Perl 5. When used, it sets up the "class" and "role"
44       keywords.
45
46       Note: Please see the "WARNING" section below!
47

KEYWORDS

49   class
50           class Foo { ... }
51
52           my $anon_class = class { ... };
53
54       Declares a new class. The class can be either named or anonymous,
55       depending on whether or not a classname is given. Within the class
56       definition Moose and MooseX::Method::Signatures are set up
57       automatically in addition to the other keywords described in this
58       document. At the end of the definition the class will be made
59       immutable. namespace::autoclean is injected to clean up Moose and other
60       imports for you.
61
62       Because of the way the options are parsed, you cannot have a class
63       named "is", "with" or "extends".
64
65       It's possible to specify options for classes:
66
67       extends
68               class Foo extends Bar { ... }
69
70           Sets a superclass for the class being declared.
71
72       with
73               class Foo with Role             { ... }
74               class Foo with Role1 with Role2 { ... }
75               class Foo with (Role1, Role2)   { ... }
76
77           Applies a role or roles to the class being declared.
78
79       is mutable
80               class Foo is mutable { ... }
81
82           Causes the class not to be made immutable after its definition.
83
84           Options can also be provided for anonymous classes using the same
85           syntax:
86
87               my $meta_class = class with Role;
88
89   role
90           role Foo { ... }
91
92           my $anon_role = role { ... };
93
94       Declares a new role. The role can be either named or anonymous,
95       depending on whether or not a name is given. Within the role definition
96       Moose::Role and MooseX::Method::Signatures are set up automatically in
97       addition to the other keywords described in this document. Again,
98       namespace::autoclean is injected to clean up Moose::Role and other
99       imports for you.
100
101       It's possible to specify options for roles:
102
103       with
104               role Foo with Bar { ... }
105
106           Applies a role to the role being declared.
107
108   before / after / around / override / augment
109           before   foo ($x, $y, $z) { ... }
110           after    bar ($x, $y, $z) { ... }
111           around   baz ($x, $y, $z) { ... }
112           override moo ($x, $y, $z) { ... }
113           augment  kuh ($x, $y, $z) { ... }
114
115       Add a method modifier. Those work like documented in Moose, except for
116       the slightly nicer syntax and the method signatures, which work like
117       documented in MooseX::Method::Signatures.
118
119       For the "around" modifier an additional argument called $orig is
120       automatically set up as the invocant for the method.
121
122   clean
123       Sometimes you don't want the automatic cleaning the "class" and "role"
124       keywords provide using namespace::autoclean. In those cases you can
125       specify the "dirty" trait for your class or role:
126
127           use MooseX::Declare;
128           class Foo is dirty { ... }
129
130       This will prevent cleaning of your namespace, except for the keywords
131       imported from "Moose" or "Moose::Role". Additionally, a "clean" keyword
132       is provided, which allows you to explicitly clean all functions that
133       were defined prior to calling "clean". Here's an example:
134
135           use MooseX::Declare;
136           class Foo is dirty {
137               sub helper_function { ... }
138               clean;
139               method foo ($stuff) { ...; return helper_function($stuff); }
140           }
141
142       With that, the helper function won't be available as a method to a user
143       of your class, but you're still able to use it inside your class.
144

NOTE ON IMPORTS

146       When creating a class with MooseX::Declare like:
147
148           use MooseX::Declare;
149           class Foo { ... }
150
151       What actually happens is something like this:
152
153           {
154               package Foo;
155               use Moose;
156               use namespace::autoclean;
157               ...
158               __PACKAGE__->meta->make_immutable;
159           }
160
161       So if you declare imports outside the class, the symbols get imported
162       into the "main::" namespace, not the class' namespace. The symbols then
163       cannot be called from within the class:
164
165           use MooseX::Declare;
166           use Data::Dump qw/dump/;
167           class Foo {
168               method dump($value) { return dump($value) } # Data::Dump::dump IS NOT in Foo::
169               method pp($value)   { $self->dump($value) } # an alias for our dump method
170           }
171
172       To solve this, only import MooseX::Declare outside the class definition
173       (because you have to). Make all other imports inside the class
174       definition.
175
176           use MooseX::Declare;
177           class Foo {
178               use Data::Dump qw/dump/;
179               method dump($value) { return dump($value) } # Data::Dump::dump IS in Foo::
180               method pp($value)   { $self->dump($value) } # an alias for our dump method
181           }
182
183           Foo->new->dump($some_value);
184           Foo->new->pp($some_value);
185
186       NOTE that the import "Data::Dump::dump()" and the method "Foo::dump()",
187       although having the same name, do not conflict with each other, because
188       the imported "dump" function will be cleaned during compile time, so
189       only the method remains there at run time. If you want to do more
190       esoteric things with imports, have a look at the "clean" keyword and
191       the "dirty" trait.
192

WARNING

194       Warning: MooseX::Declare is based on Devel::Declare, a giant bag of
195       crack originally implemented by mst with the goal of upsetting the perl
196       core developers so much by its very existence that they implemented
197       proper keyword handling in the core.
198
199       As of perl5 version 14, this goal has been achieved, and modules such
200       as Devel::CallParser, Function::Parameters, and Keyword::Simple provide
201       mechanisms to mangle perl syntax that don't require hallucinogenic
202       drugs to interpret the error messages they produce.
203
204       If you want to use declarative syntax in new code, please for the love
205       of kittens get yourself a recent perl and look at Moops instead.
206

SEE ALSO

208       ·   Moose
209
210       ·   Moose::Role
211
212       ·   MooseX::Method::Signatures
213
214       ·   namespace::autoclean
215
216       ·   vim syntax: <http://www.vim.org/scripts/script.php?script_id=2526>
217
218       ·   emacs syntax: <http://github.com/jrockway/cperl-mode>
219
220       ·   Geany syntax + notes:
221           <http://www.cattlegrid.info/blog/2009/09/moosex-declare-geany-syntax.html>
222
223       ·   Devel::CallParser
224
225       ·   Function::Parameters
226
227       ·   Keyword::Simple
228
229       ·   Moops
230

AUTHOR

232       Florian Ragwitz <rafl@debian.org>
233

CONTRIBUTORS

235       ·   Karen Etheridge <ether@cpan.org>
236
237       ·   Piers Cawley <pdcawley@bofh.org.uk>
238
239       ·   Robert 'phaylon' Sedlacek <rs@474.at>
240
241       ·   Ash Berlin <ash_github@firemirror.com>
242
243       ·   Nick Perez <nperez@cpan.org>
244
245       ·   Nelo Onyiah <nelo.onyiah@gmail.com>
246
247       ·   Chas. J. Owens IV <chas.owens@gmail.com>
248
249       ·   leedo <lee@laylward.com>
250
251       ·   Michele Beltrame <arthas@cpan.org>
252
253       ·   Frank Wiegand <fwie@cpan.org>
254
255       ·   David Steinbrunner <dsteinbrunner@pobox.com>
256
257       ·   Oleg Kostyuk <cub.uanic@gmail.com>
258
259       ·   Dave Rolsky <autarch@urth.org>
260
261       ·   Rafael Kitover <rkitover@io.com>
262
263       ·   Chris Prather <chris@prather.org>
264
265       ·   Stevan Little <stevan.little@iinteractive.com>
266
267       ·   Tomas Doran <bobtfish@bobtfish.net>
268
269       ·   Yanick Champoux <yanick@babyl.dyndns.org>
270
271       ·   Justin Hunter <justin.d.hunter@gmail.com>
272
274       This software is copyright (c) 2008 by Florian Ragwitz.
275
276       This is free software; you can redistribute it and/or modify it under
277       the same terms as the Perl 5 programming language system itself.
278
279
280
281perl v5.32.0                      2020-07-28              MooseX::Declare(3pm)
Impressum