Commit | Line | Data |
1d8f590f |
1 | =pod |
2 | |
3 | =head1 NAME |
4 | |
d67ce58f |
5 | Moose::Manual::MooseX - Recommended Moose extensions |
1d8f590f |
6 | |
7 | =head1 MooseX? |
8 | |
1d9599cb |
9 | It's easy to extend and change Moose, and this is part of what makes |
10 | Moose so powerful. You can use the MOP API to do things your own way, |
1d8f590f |
11 | add new features, and generally customize your Moose. |
12 | |
13 | Writing your own extensions does require a good understanding of the |
1d9599cb |
14 | meta-model. You can start learning about this with the |
dab94063 |
15 | L<Moose::Manual::MOP> docs. There are also several extension recipes |
1d9599cb |
16 | in the L<Moose::Cookbook>. |
1d8f590f |
17 | |
18 | Explaining how to write extensions is beyond the scope of this |
19 | manual. Fortunately, lots of people have already written extensions |
20 | and put them on CPAN for you. |
21 | |
22 | This document covers a few of the ones we like best. |
23 | |
d67ce58f |
24 | =head1 L<MooseX::AttributeHelpers> |
1d8f590f |
25 | |
1d9599cb |
26 | If you only look at one extension, it should be this one. It provides |
27 | the equivalent of delegation for all of Perl's native data types, such |
28 | as array reference, hash references, numbers, strings, etc. |
1d8f590f |
29 | |
30 | This lets you create I<much> cleaner and fluent APIs. |
31 | |
32 | package User; |
33 | |
34 | use Moose; |
35 | use MooseX::AttributeHelpers; |
36 | |
37 | has '_orders' => ( |
38 | metaclass => 'Collection::Array', |
39 | is => 'ro', |
40 | isa => 'ArrayRef', |
41 | default => sub { [] }, |
42 | provides => { |
43 | push => 'add_order', |
44 | shift => 'next_order', |
45 | elements => 'orders', |
46 | }, |
47 | ); |
48 | |
49 | Instead of directly exposing an array reference, we have three |
50 | well-named, easy to use methods. |
51 | |
a434262f |
52 | =head1 L<Moose::Autobox> |
53 | |
54 | MooseX::AttributeHelpers, but turned inside out, Moose::Autobox provides |
55 | methods on both arrays/hashes/etc. but also references to them, using |
56 | Moose roles, allowing you do to things like: |
57 | |
58 | use Moose::Autobox; |
59 | |
60 | $somebody_elses_object->orders->push($order); |
61 | |
62 | Lexically scoped and not to everybody's taste, but very handy for sugaring |
63 | up other people's APIs and your own code. |
64 | |
d67ce58f |
65 | =head1 L<MooseX::StrictConstructor> |
1d8f590f |
66 | |
67 | By default, Moose lets you pass any old junk into a class's |
0c39debe |
68 | constructor. If you load L<MooseX::StrictConstructor>, your class will |
1d8f590f |
69 | throw an error if it sees something it doesn't recognize; |
70 | |
71 | package User; |
72 | |
73 | use Moose; |
74 | use MooseX::StrictConstructor; |
75 | |
76 | has 'name'; |
77 | has 'email'; |
78 | |
79 | User->new( name => 'Bob', emali => 'bob@example.com' ); |
80 | |
0c39debe |
81 | With L<MooseX::StrictConstructor>, that typo ("emali") will cause a |
1d9599cb |
82 | runtime error. With plain old Moose, the "emali" attribute would be |
83 | silently ignored. |
1d8f590f |
84 | |
d67ce58f |
85 | =head1 L<MooseX::Params::Validate> |
1d8f590f |
86 | |
0c39debe |
87 | We have high hopes for the future of L<MooseX::Method::Signatures> and |
a434262f |
88 | L<MooseX::Declare>. However, these modules, while used regularly in |
89 | production by some of the more insane members of the community, are |
90 | still marked alpha just in case backwards incompatible changes need to |
91 | be made. |
92 | |
93 | If you don't want to risk that, for now we recommend the decidedly more |
0c39debe |
94 | clunky (but also faster and simpler) L<MooseX::Params::Validate>. This |
1d8f590f |
95 | module lets you apply Moose types and coercions to any method |
96 | arguments. |
97 | |
98 | package User; |
99 | |
100 | use Moose; |
5a3fb5fc |
101 | use MooseX::Params::Validate; |
1d8f590f |
102 | |
103 | sub login { |
104 | my $self = shift; |
105 | my ($password) |
1d9599cb |
106 | = validated_list( \@_, password => { isa => 'Str', required => 1 } ); |
1d8f590f |
107 | |
108 | ... |
109 | } |
110 | |
d67ce58f |
111 | =head1 L<MooseX::Getopt> |
1d8f590f |
112 | |
113 | This is a role which adds a C<new_with_options> method to your |
114 | class. This is a constructor that takes the command line options and |
115 | uses them to populate attributes. |
116 | |
0f62a437 |
117 | This makes writing a command-line application as a module trivially |
1d8f590f |
118 | simple: |
119 | |
120 | package App::Foo; |
121 | |
122 | use Moose; |
123 | with 'MooseX::Getopt'; |
124 | |
125 | has 'input' => ( |
126 | is => 'ro', |
127 | isa => 'Str', |
128 | required => 1 |
129 | ); |
130 | |
131 | has 'output' => ( |
132 | is => 'ro', |
133 | isa => 'Str', |
134 | required => 1 |
135 | ); |
136 | |
137 | sub run { ... } |
138 | |
139 | Then in the script that gets run we have: |
140 | |
141 | use App::Foo; |
142 | |
143 | App::Foo->new_with_options->run; |
144 | |
1d9599cb |
145 | From the command line, someone can execute the script: |
146 | |
147 | foo@example> foo --input /path/to/input --output /path/to/output |
148 | |
d67ce58f |
149 | =head1 L<MooseX::Singleton> |
1d8f590f |
150 | |
a434262f |
151 | To be honest, using a singleton is just a way to have a magic global |
152 | variable in languages that don't actually have global variables. |
153 | |
154 | In perl, you should almost certaintly just use a global. |
155 | |
156 | However, if your colleagues are too used to java to understand that a |
157 | singleton is a slow, stupid way of hacking around its lack of globals, |
158 | L<MooseX::Singleton> lets you have a Moose class that's a singleton: |
1d8f590f |
159 | |
160 | package Config; |
161 | |
162 | use MooseX::Singleton; # instead of Moose |
163 | |
164 | has 'cache_dir' => ( ... ); |
165 | |
166 | It's that simple. |
167 | |
168 | =head1 EXTENSIONS TO CONSIDER |
169 | |
1d9599cb |
170 | There are literally dozens of other extensions on CPAN. This is a list |
171 | of extensions that you might find useful, but we're not quite ready to |
172 | endorse just yet. |
1d8f590f |
173 | |
d67ce58f |
174 | =head2 L<MooseX::Declare> |
1d8f590f |
175 | |
176 | Extends Perl with Moose-based keywords using C<Devel::Declare>. Very |
177 | cool, but still new and experimental. |
178 | |
179 | class User { |
180 | |
181 | has 'name' => ( ... ); |
182 | has 'email' => ( ... ); |
183 | |
184 | method login (Str $password) { ... } |
185 | } |
186 | |
d67ce58f |
187 | =head2 L<MooseX::Types> |
1d8f590f |
188 | |
189 | This extension helps you build a type library for your application. It |
6549b0d1 |
190 | also lets you predeclare type names and use them as barewords. |
1d8f590f |
191 | |
5a3fb5fc |
192 | use MooseX::Types -declare => ['PositiveInt']; |
1d8f590f |
193 | use MooseX::Types::Moose 'Int'; |
194 | |
195 | subtype PositiveInt |
196 | => as Int, |
197 | => where { $_ > 0 } |
198 | => message {"Int is not larger than 0"}; |
199 | |
1d9599cb |
200 | One nice feature is that those bareword names are actually namespaced |
1d8f590f |
201 | in Moose's type registry, so multiple applications can use the same |
202 | bareword names, even if the type definitions differ. |
203 | |
d67ce58f |
204 | =head2 L<MooseX::Types::Structured> |
1d8f590f |
205 | |
0c39debe |
206 | This extension builds on top of L<MooseX::Types> to let you declare |
1d8f590f |
207 | complex data structure types. |
208 | |
209 | use MooseX::Types -declare => [ qw( Name Color ) ]; |
210 | use MooseX::Types::Moose qw(Str Int); |
211 | use MooseX::Types::Structured qw(Dict Tuple Optional); |
212 | |
213 | subtype Name |
214 | => as Dict[ first => Str, middle => Optional[Str], last => Str ]; |
215 | |
216 | subtype Color |
217 | => as Tuple[ Int, Int, Int, Optional[Int] ]; |
218 | |
219 | Of course, you could always use objects to represent these sorts of |
220 | things too. |
221 | |
d67ce58f |
222 | =head2 L<MooseX::ClassAttribute> |
1d8f590f |
223 | |
224 | This extension provides class attributes for Moose classes. The |
225 | declared class attributes are introspectable just like regular Moose |
226 | attributes. |
227 | |
228 | package User; |
229 | |
230 | use Moose; |
231 | use MooseX::ClassAttribute; |
232 | |
233 | has 'name' => ( ... ); |
234 | |
235 | class_has 'Cache' => ( ... ); |
236 | |
a434262f |
237 | Note however that this class attribute does -not- inherit like a |
238 | L<Class::Data::Inheritable> or similar attribute - calling |
239 | |
240 | $subclass->Cache($cache); |
241 | |
242 | will set it for the superclass as well. Additionally, class data is usually |
243 | The Wrong Thing To Do in a strongly OO program since it makes testing a |
244 | lot harder - consider carefully whether you'd be better off with an object |
245 | that's passed around instead. |
246 | |
d67ce58f |
247 | =head2 L<MooseX::Daemonize> |
1d8f590f |
248 | |
249 | This is a role that provides a number of methods useful for creating a |
250 | daemon, including methods for starting and stopping, managing a PID |
251 | file, and signal handling. |
252 | |
d67ce58f |
253 | =head2 L<MooseX::Role::Parameterized> |
1d8f590f |
254 | |
255 | If you find yourself wanting a role that customizes itself for each |
256 | consumer, this is the tool for you. With this module, you can create a |
5a3fb5fc |
257 | role that accepts parameters and generates attributes, methods, etc. on |
1d8f590f |
258 | a customized basis for each consumer. |
259 | |
d67ce58f |
260 | =head2 L<MooseX::POE> |
1d8f590f |
261 | |
262 | This is a small wrapper that ties together a Moose class with |
263 | C<POE::Session>, and gives you an C<event> sugar function to declare |
264 | event handlers. |
265 | |
d67ce58f |
266 | =head2 L<MooseX::FollowPBP> |
ddf2636a |
267 | |
268 | Automatically names all accessors I<Perl Best Practices>-style, |
1d9599cb |
269 | "get_size" and "set_size". |
ddf2636a |
270 | |
d67ce58f |
271 | =head2 L<MooseX::SemiAffordanceAccessor> |
ddf2636a |
272 | |
1d9599cb |
273 | Automatically names all accessors with an explicit set and implicit |
274 | get, "size" and "set_size". |
ddf2636a |
275 | |
1d8f590f |
276 | =head1 AUTHOR |
277 | |
278 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
279 | |
280 | =head1 COPYRIGHT AND LICENSE |
281 | |
2840a3b2 |
282 | Copyright 2009 by Infinity Interactive, Inc. |
1d8f590f |
283 | |
284 | L<http://www.iinteractive.com> |
285 | |
286 | This library is free software; you can redistribute it and/or modify |
287 | it under the same terms as Perl itself. |
288 | |
289 | =cut |