Move methods so they are in order of use
[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
15L<Moose::Manual::MOP> docs. There are also several extensions recipes
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
d67ce58f 52=head1 L<MooseX::StrictConstructor>
1d8f590f 53
54By default, Moose lets you pass any old junk into a class's
0c39debe 55constructor. If you load L<MooseX::StrictConstructor>, your class will
1d8f590f 56throw an error if it sees something it doesn't recognize;
57
58 package User;
59
60 use Moose;
61 use MooseX::StrictConstructor;
62
63 has 'name';
64 has 'email';
65
66 User->new( name => 'Bob', emali => 'bob@example.com' );
67
0c39debe 68With L<MooseX::StrictConstructor>, that typo ("emali") will cause a
1d9599cb 69runtime error. With plain old Moose, the "emali" attribute would be
70silently ignored.
1d8f590f 71
d67ce58f 72=head1 L<MooseX::Params::Validate>
1d8f590f 73
0c39debe 74We have high hopes for the future of L<MooseX::Method::Signatures> and
75L<MooseX::Declare>. However, for now we recommend the decidedly more
76clunky (but also faster and simpler) L<MooseX::Params::Validate>. This
1d8f590f 77module lets you apply Moose types and coercions to any method
78arguments.
79
80 package User;
81
82 use Moose;
83 use MooseX::Params::Validate qw( validatep );
84
85 sub login {
86 my $self = shift;
87 my ($password)
1d9599cb 88 = validated_list( \@_, password => { isa => 'Str', required => 1 } );
1d8f590f 89
90 ...
91 }
92
d67ce58f 93=head1 L<MooseX::Getopt>
1d8f590f 94
95This is a role which adds a C<new_with_options> method to your
96class. This is a constructor that takes the command line options and
97uses them to populate attributes.
98
0f62a437 99This makes writing a command-line application as a module trivially
1d8f590f 100simple:
101
102 package App::Foo;
103
104 use Moose;
105 with 'MooseX::Getopt';
106
107 has 'input' => (
108 is => 'ro',
109 isa => 'Str',
110 required => 1
111 );
112
113 has 'output' => (
114 is => 'ro',
115 isa => 'Str',
116 required => 1
117 );
118
119 sub run { ... }
120
121Then in the script that gets run we have:
122
123 use App::Foo;
124
125 App::Foo->new_with_options->run;
126
1d9599cb 127From the command line, someone can execute the script:
128
129 foo@example> foo --input /path/to/input --output /path/to/output
130
d67ce58f 131=head1 L<MooseX::Singleton>
1d8f590f 132
133To be honest, using a singleton is often a hack, but it sure is a
0c39debe 134handy hack. L<MooseX::Singleton> lets you have a Moose class that's a
1d8f590f 135singleton:
136
137 package Config;
138
139 use MooseX::Singleton; # instead of Moose
140
141 has 'cache_dir' => ( ... );
142
143It's that simple.
144
145=head1 EXTENSIONS TO CONSIDER
146
1d9599cb 147There are literally dozens of other extensions on CPAN. This is a list
148of extensions that you might find useful, but we're not quite ready to
149endorse just yet.
1d8f590f 150
d67ce58f 151=head2 L<MooseX::Declare>
1d8f590f 152
153Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
154cool, but still new and experimental.
155
156 class User {
157
158 has 'name' => ( ... );
159 has 'email' => ( ... );
160
161 method login (Str $password) { ... }
162 }
163
d67ce58f 164=head2 L<MooseX::Types>
1d8f590f 165
166This extension helps you build a type library for your application. It
6549b0d1 167also lets you predeclare type names and use them as barewords.
1d8f590f 168
169 use MooseX::Types -declare => ['PosInt'];
170 use MooseX::Types::Moose 'Int';
171
172 subtype PositiveInt
173 => as Int,
174 => where { $_ > 0 }
175 => message {"Int is not larger than 0"};
176
1d9599cb 177One nice feature is that those bareword names are actually namespaced
1d8f590f 178in Moose's type registry, so multiple applications can use the same
179bareword names, even if the type definitions differ.
180
d67ce58f 181=head2 L<MooseX::Types::Structured>
1d8f590f 182
0c39debe 183This extension builds on top of L<MooseX::Types> to let you declare
1d8f590f 184complex data structure types.
185
186 use MooseX::Types -declare => [ qw( Name Color ) ];
187 use MooseX::Types::Moose qw(Str Int);
188 use MooseX::Types::Structured qw(Dict Tuple Optional);
189
190 subtype Name
191 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
192
193 subtype Color
194 => as Tuple[ Int, Int, Int, Optional[Int] ];
195
196Of course, you could always use objects to represent these sorts of
197things too.
198
d67ce58f 199=head2 L<MooseX::ClassAttribute>
1d8f590f 200
201This extension provides class attributes for Moose classes. The
202declared class attributes are introspectable just like regular Moose
203attributes.
204
205 package User;
206
207 use Moose;
208 use MooseX::ClassAttribute;
209
210 has 'name' => ( ... );
211
212 class_has 'Cache' => ( ... );
213
d67ce58f 214=head2 L<MooseX::Daemonize>
1d8f590f 215
216This is a role that provides a number of methods useful for creating a
217daemon, including methods for starting and stopping, managing a PID
218file, and signal handling.
219
d67ce58f 220=head2 L<MooseX::Role::Parameterized>
1d8f590f 221
222If you find yourself wanting a role that customizes itself for each
223consumer, this is the tool for you. With this module, you can create a
224role that accepts parameters and generates attributes, methods, etc on
225a customized basis for each consumer.
226
d67ce58f 227=head2 L<MooseX::POE>
1d8f590f 228
229This is a small wrapper that ties together a Moose class with
230C<POE::Session>, and gives you an C<event> sugar function to declare
231event handlers.
232
d67ce58f 233=head2 L<MooseX::FollowPBP>
ddf2636a 234
235Automatically names all accessors I<Perl Best Practices>-style,
1d9599cb 236"get_size" and "set_size".
ddf2636a 237
d67ce58f 238=head2 L<MooseX::SemiAffordanceAccessor>
ddf2636a 239
1d9599cb 240Automatically names all accessors with an explicit set and implicit
241get, "size" and "set_size".
ddf2636a 242
1d8f590f 243=head1 AUTHOR
244
245Dave Rolsky E<lt>autarch@urth.orgE<gt>
246
247=head1 COPYRIGHT AND LICENSE
248
2840a3b2 249Copyright 2009 by Infinity Interactive, Inc.
1d8f590f 250
251L<http://www.iinteractive.com>
252
253This library is free software; you can redistribute it and/or modify
254it under the same terms as Perl itself.
255
256=cut