add some notes about a few extensions
[gitmo/Moose.git] / lib / Moose / Manual / MooseX.pod
CommitLineData
1d8f590f 1=pod
2
3=head1 NAME
4
d67ce58f 5Moose::Manual::MooseX - Recommended Moose extensions
1d8f590f 6
7=head1 MooseX?
8
1d9599cb 9It's easy to extend and change Moose, and this is part of what makes
10Moose so powerful. You can use the MOP API to do things your own way,
1d8f590f 11add new features, and generally customize your Moose.
12
13Writing your own extensions does require a good understanding of the
1d9599cb 14meta-model. You can start learning about this with the
dab94063 15L<Moose::Manual::MOP> docs. There are also several extension recipes
1d9599cb 16in the L<Moose::Cookbook>.
1d8f590f 17
18Explaining how to write extensions is beyond the scope of this
19manual. Fortunately, lots of people have already written extensions
20and put them on CPAN for you.
21
22This document covers a few of the ones we like best.
23
d67ce58f 24=head1 L<MooseX::AttributeHelpers>
1d8f590f 25
1d9599cb 26If you only look at one extension, it should be this one. It provides
27the equivalent of delegation for all of Perl's native data types, such
28as array reference, hash references, numbers, strings, etc.
1d8f590f 29
30This 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
49Instead of directly exposing an array reference, we have three
50well-named, easy to use methods.
51
a434262f 52=head1 L<Moose::Autobox>
53
54MooseX::AttributeHelpers, but turned inside out, Moose::Autobox provides
55methods on both arrays/hashes/etc. but also references to them, using
56Moose roles, allowing you do to things like:
57
58 use Moose::Autobox;
59
60 $somebody_elses_object->orders->push($order);
61
62Lexically scoped and not to everybody's taste, but very handy for sugaring
63up other people's APIs and your own code.
64
d67ce58f 65=head1 L<MooseX::StrictConstructor>
1d8f590f 66
67By default, Moose lets you pass any old junk into a class's
0c39debe 68constructor. If you load L<MooseX::StrictConstructor>, your class will
1d8f590f 69throw 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 81With L<MooseX::StrictConstructor>, that typo ("emali") will cause a
1d9599cb 82runtime error. With plain old Moose, the "emali" attribute would be
83silently ignored.
1d8f590f 84
d67ce58f 85=head1 L<MooseX::Params::Validate>
1d8f590f 86
0c39debe 87We have high hopes for the future of L<MooseX::Method::Signatures> and
a434262f 88L<MooseX::Declare>. However, these modules, while used regularly in
89production by some of the more insane members of the community, are
90still marked alpha just in case backwards incompatible changes need to
91be made.
92
93If you don't want to risk that, for now we recommend the decidedly more
0c39debe 94clunky (but also faster and simpler) L<MooseX::Params::Validate>. This
1d8f590f 95module lets you apply Moose types and coercions to any method
96arguments.
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
113This is a role which adds a C<new_with_options> method to your
114class. This is a constructor that takes the command line options and
115uses them to populate attributes.
116
0f62a437 117This makes writing a command-line application as a module trivially
1d8f590f 118simple:
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
139Then in the script that gets run we have:
140
141 use App::Foo;
142
143 App::Foo->new_with_options->run;
144
1d9599cb 145From 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 151To be honest, using a singleton is just a way to have a magic global
152variable in languages that don't actually have global variables.
153
154In perl, you should almost certaintly just use a global.
155
156However, if your colleagues are too used to java to understand that a
157singleton is a slow, stupid way of hacking around its lack of globals,
158L<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
166It's that simple.
167
168=head1 EXTENSIONS TO CONSIDER
169
1d9599cb 170There are literally dozens of other extensions on CPAN. This is a list
171of extensions that you might find useful, but we're not quite ready to
172endorse just yet.
1d8f590f 173
d67ce58f 174=head2 L<MooseX::Declare>
1d8f590f 175
176Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
177cool, 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
189This extension helps you build a type library for your application. It
6549b0d1 190also 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 200One nice feature is that those bareword names are actually namespaced
1d8f590f 201in Moose's type registry, so multiple applications can use the same
202bareword names, even if the type definitions differ.
203
d67ce58f 204=head2 L<MooseX::Types::Structured>
1d8f590f 205
0c39debe 206This extension builds on top of L<MooseX::Types> to let you declare
1d8f590f 207complex 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
219Of course, you could always use objects to represent these sorts of
220things too.
221
d67ce58f 222=head2 L<MooseX::ClassAttribute>
1d8f590f 223
224This extension provides class attributes for Moose classes. The
225declared class attributes are introspectable just like regular Moose
226attributes.
227
228 package User;
229
230 use Moose;
231 use MooseX::ClassAttribute;
232
233 has 'name' => ( ... );
234
235 class_has 'Cache' => ( ... );
236
a434262f 237Note however that this class attribute does -not- inherit like a
238L<Class::Data::Inheritable> or similar attribute - calling
239
240 $subclass->Cache($cache);
241
242will set it for the superclass as well. Additionally, class data is usually
243The Wrong Thing To Do in a strongly OO program since it makes testing a
244lot harder - consider carefully whether you'd be better off with an object
245that's passed around instead.
246
d67ce58f 247=head2 L<MooseX::Daemonize>
1d8f590f 248
249This is a role that provides a number of methods useful for creating a
250daemon, including methods for starting and stopping, managing a PID
251file, and signal handling.
252
d67ce58f 253=head2 L<MooseX::Role::Parameterized>
1d8f590f 254
255If you find yourself wanting a role that customizes itself for each
256consumer, this is the tool for you. With this module, you can create a
5a3fb5fc 257role that accepts parameters and generates attributes, methods, etc. on
1d8f590f 258a customized basis for each consumer.
259
d67ce58f 260=head2 L<MooseX::POE>
1d8f590f 261
262This is a small wrapper that ties together a Moose class with
263C<POE::Session>, and gives you an C<event> sugar function to declare
264event handlers.
265
d67ce58f 266=head2 L<MooseX::FollowPBP>
ddf2636a 267
268Automatically 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 273Automatically names all accessors with an explicit set and implicit
274get, "size" and "set_size".
ddf2636a 275
1d8f590f 276=head1 AUTHOR
277
278Dave Rolsky E<lt>autarch@urth.orgE<gt>
279
280=head1 COPYRIGHT AND LICENSE
281
2840a3b2 282Copyright 2009 by Infinity Interactive, Inc.
1d8f590f 283
284L<http://www.iinteractive.com>
285
286This library is free software; you can redistribute it and/or modify
287it under the same terms as Perl itself.
288
289=cut