bump copyright date to 2009
[gitmo/Moose.git] / lib / Moose / Manual / MooseX.pod
CommitLineData
1d8f590f 1=pod
2
3=head1 NAME
4
5Moose::Manual::MooseX - Recommended Moose Extensions
6
7=head1 MooseX?
8
9One of the great things about Moose is that it is easy to extend and
10override. You can use the meta-model API to do things your own way,
11add new features, and generally customize your Moose.
12
13Writing your own extensions does require a good understanding of the
14meta-model. You can start learning about thiswith the
15L<Moose::Manual::Introspection> docs. There are also several extensions
16recipes in the L<Moose::Cookbook>.
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
24=head1 MooseX::AttributeHelpers
25
26If you only look at one extension, it should be this one. The name
27isn't the best, but what it does it provide the equivalent of
28delegation for all of Perl's native data types, such as array
29reference, hash references, numbers, strings, etc.
30
31This lets you create I<much> cleaner and fluent APIs.
32
33 package User;
34
35 use Moose;
36 use MooseX::AttributeHelpers;
37
38 has '_orders' => (
39 metaclass => 'Collection::Array',
40 is => 'ro',
41 isa => 'ArrayRef',
42 default => sub { [] },
43 provides => {
44 push => 'add_order',
45 shift => 'next_order',
46 elements => 'orders',
47 },
48 );
49
50Instead of directly exposing an array reference, we have three
51well-named, easy to use methods.
52
53=head1 MooseX::StrictConstructor
54
55By default, Moose lets you pass any old junk into a class's
56constructor. If you load C<MooseX::StrictConstructor>, your class will
57throw an error if it sees something it doesn't recognize;
58
59 package User;
60
61 use Moose;
62 use MooseX::StrictConstructor;
63
64 has 'name';
65 has 'email';
66
67 User->new( name => 'Bob', emali => 'bob@example.com' );
68
69With C<MooseX::StrictConstructor>, that typo ("emali") will cause a
70runtime error. Otherwise, the "emali" attribute would just be silently
71ignored.
72
73=head1 MooseX::Params::Validate
74
75We have high hopes for the future of C<MooseX::Method::Signatures> and
76C<MooseX::Declare>. However, for now we recommend the decidely more
77clunky (but also faster and simpler) C<MooseX::Params::Validate>. This
78module lets you apply Moose types and coercions to any method
79arguments.
80
81 package User;
82
83 use Moose;
84 use MooseX::Params::Validate qw( validatep );
85
86 sub login {
87 my $self = shift;
88 my ($password)
89 = validatep( \@_, password => { isa => 'Str', required => 1 } );
90
91 ...
92 }
93
94=head1 MooseX::Getopt
95
96This is a role which adds a C<new_with_options> method to your
97class. This is a constructor that takes the command line options and
98uses them to populate attributes.
99
100Thia makes writing a command-line application as a module trivially
101simple:
102
103 package App::Foo;
104
105 use Moose;
106 with 'MooseX::Getopt';
107
108 has 'input' => (
109 is => 'ro',
110 isa => 'Str',
111 required => 1
112 );
113
114 has 'output' => (
115 is => 'ro',
116 isa => 'Str',
117 required => 1
118 );
119
120 sub run { ... }
121
122Then in the script that gets run we have:
123
124 use App::Foo;
125
126 App::Foo->new_with_options->run;
127
128=head1 MooseX::Singleton
129
130To be honest, using a singleton is often a hack, but it sure is a
131handy hack. C<MooseX::Singleton> lets you have a Moose class that's a
132singleton:
133
134 package Config;
135
136 use MooseX::Singleton; # instead of Moose
137
138 has 'cache_dir' => ( ... );
139
140It's that simple.
141
142=head1 EXTENSIONS TO CONSIDER
143
144There are literally dozens of other extensions on CPAN. These are a
145few to consider. We're not quite ready to recommend them outright,
146though. There's a couple reasons for this. One, they may be very
147specialized. Two, they may be immature. Three, they may not be quite
148right yet. Four, we may not all agree they're such a great idea.
149
150=head2 MooseX::Declare
151
152Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
153cool, but still new and experimental.
154
155 class User {
156
157 has 'name' => ( ... );
158 has 'email' => ( ... );
159
160 method login (Str $password) { ... }
161 }
162
163=head2 MooseX::Types
164
165This extension helps you build a type library for your application. It
166also lets you pre-declare type names and use them as barewords.
167
168 use MooseX::Types -declare => ['PosInt'];
169 use MooseX::Types::Moose 'Int';
170
171 subtype PositiveInt
172 => as Int,
173 => where { $_ > 0 }
174 => message {"Int is not larger than 0"};
175
176One nice feature is the those bareword names are actually namespaces
177in Moose's type registry, so multiple applications can use the same
178bareword names, even if the type definitions differ.
179
180=head2 MooseX::Types::Structured
181
182This extension builds on top of C<MooseX::Types> to let you declare
183complex data structure types.
184
185 use MooseX::Types -declare => [ qw( Name Color ) ];
186 use MooseX::Types::Moose qw(Str Int);
187 use MooseX::Types::Structured qw(Dict Tuple Optional);
188
189 subtype Name
190 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
191
192 subtype Color
193 => as Tuple[ Int, Int, Int, Optional[Int] ];
194
195Of course, you could always use objects to represent these sorts of
196things too.
197
198=head2 MooseX::ClassAttribute
199
200This extension provides class attributes for Moose classes. The
201declared class attributes are introspectable just like regular Moose
202attributes.
203
204 package User;
205
206 use Moose;
207 use MooseX::ClassAttribute;
208
209 has 'name' => ( ... );
210
211 class_has 'Cache' => ( ... );
212
213=head2 MooseX::Daemonize
214
215This is a role that provides a number of methods useful for creating a
216daemon, including methods for starting and stopping, managing a PID
217file, and signal handling.
218
219=head2 MooseX::Role::Parameterized
220
221If you find yourself wanting a role that customizes itself for each
222consumer, this is the tool for you. With this module, you can create a
223role that accepts parameters and generates attributes, methods, etc on
224a customized basis for each consumer.
225
226=head2 MooseX::POE
227
228This is a small wrapper that ties together a Moose class with
229C<POE::Session>, and gives you an C<event> sugar function to declare
230event handlers.
231
ddf2636a 232=head2 MooseX::FollowPBP
233
234Automatically names all accessors I<Perl Best Practices>-style,
235"get_name" and "set_name".
236
237=head2 MooseX::SemiAffordanceAccessor
238
239Autoamtically names all accessors with an explicit set and implicit
240get, "name" and "set_name".
241
1d8f590f 242=head1 AUTHOR
243
244Dave Rolsky E<lt>autarch@urth.orgE<gt>
245
246=head1 COPYRIGHT AND LICENSE
247
2840a3b2 248Copyright 2009 by Infinity Interactive, Inc.
1d8f590f 249
250L<http://www.iinteractive.com>
251
252This library is free software; you can redistribute it and/or modify
253it under the same terms as Perl itself.
254
255=cut