correct pod document name
[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
e24c59ad 154In perl, you can just as easily use a global. However, if your
155colleagues are Java-infected, they might prefer a singleton. Also, if
156you have an existing class that I<isn't> a singleton but should be,
157using L<MooseX::Singleton> is the easiest way to convert it.
1d8f590f 158
159 package Config;
160
161 use MooseX::Singleton; # instead of Moose
162
163 has 'cache_dir' => ( ... );
164
165It's that simple.
166
167=head1 EXTENSIONS TO CONSIDER
168
1d9599cb 169There are literally dozens of other extensions on CPAN. This is a list
170of extensions that you might find useful, but we're not quite ready to
171endorse just yet.
1d8f590f 172
d67ce58f 173=head2 L<MooseX::Declare>
1d8f590f 174
175Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
176cool, but still new and experimental.
177
178 class User {
179
180 has 'name' => ( ... );
181 has 'email' => ( ... );
182
183 method login (Str $password) { ... }
184 }
185
d67ce58f 186=head2 L<MooseX::Types>
1d8f590f 187
188This extension helps you build a type library for your application. It
6549b0d1 189also lets you predeclare type names and use them as barewords.
1d8f590f 190
5a3fb5fc 191 use MooseX::Types -declare => ['PositiveInt'];
1d8f590f 192 use MooseX::Types::Moose 'Int';
193
194 subtype PositiveInt
195 => as Int,
196 => where { $_ > 0 }
197 => message {"Int is not larger than 0"};
198
1d9599cb 199One nice feature is that those bareword names are actually namespaced
1d8f590f 200in Moose's type registry, so multiple applications can use the same
201bareword names, even if the type definitions differ.
202
d67ce58f 203=head2 L<MooseX::Types::Structured>
1d8f590f 204
0c39debe 205This extension builds on top of L<MooseX::Types> to let you declare
1d8f590f 206complex data structure types.
207
208 use MooseX::Types -declare => [ qw( Name Color ) ];
209 use MooseX::Types::Moose qw(Str Int);
210 use MooseX::Types::Structured qw(Dict Tuple Optional);
211
212 subtype Name
213 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
214
215 subtype Color
216 => as Tuple[ Int, Int, Int, Optional[Int] ];
217
218Of course, you could always use objects to represent these sorts of
219things too.
220
d67ce58f 221=head2 L<MooseX::ClassAttribute>
1d8f590f 222
223This extension provides class attributes for Moose classes. The
224declared class attributes are introspectable just like regular Moose
225attributes.
226
227 package User;
228
229 use Moose;
230 use MooseX::ClassAttribute;
231
232 has 'name' => ( ... );
233
234 class_has 'Cache' => ( ... );
235
a434262f 236Note however that this class attribute does -not- inherit like a
237L<Class::Data::Inheritable> or similar attribute - calling
238
239 $subclass->Cache($cache);
240
241will set it for the superclass as well. Additionally, class data is usually
242The Wrong Thing To Do in a strongly OO program since it makes testing a
243lot harder - consider carefully whether you'd be better off with an object
244that's passed around instead.
245
d67ce58f 246=head2 L<MooseX::Daemonize>
1d8f590f 247
248This is a role that provides a number of methods useful for creating a
249daemon, including methods for starting and stopping, managing a PID
250file, and signal handling.
251
d67ce58f 252=head2 L<MooseX::Role::Parameterized>
1d8f590f 253
254If you find yourself wanting a role that customizes itself for each
255consumer, this is the tool for you. With this module, you can create a
5a3fb5fc 256role that accepts parameters and generates attributes, methods, etc. on
1d8f590f 257a customized basis for each consumer.
258
d67ce58f 259=head2 L<MooseX::POE>
1d8f590f 260
261This is a small wrapper that ties together a Moose class with
262C<POE::Session>, and gives you an C<event> sugar function to declare
263event handlers.
264
d67ce58f 265=head2 L<MooseX::FollowPBP>
ddf2636a 266
267Automatically names all accessors I<Perl Best Practices>-style,
1d9599cb 268"get_size" and "set_size".
ddf2636a 269
d67ce58f 270=head2 L<MooseX::SemiAffordanceAccessor>
ddf2636a 271
1d9599cb 272Automatically names all accessors with an explicit set and implicit
273get, "size" and "set_size".
ddf2636a 274
1d8f590f 275=head1 AUTHOR
276
277Dave Rolsky E<lt>autarch@urth.orgE<gt>
278
279=head1 COPYRIGHT AND LICENSE
280
2840a3b2 281Copyright 2009 by Infinity Interactive, Inc.
1d8f590f 282
283L<http://www.iinteractive.com>
284
285This library is free software; you can redistribute it and/or modify
286it under the same terms as Perl itself.
287
288=cut