5 Moose::Manual::MooseX - Recommended Moose extensions
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,
11 add new features, and generally customize your Moose.
13 Writing your own extensions does require a good understanding of the
14 meta-model. You can start learning about this with the
15 L<Moose::Manual::MOP> docs. There are also several extension recipes
16 in the L<Moose::Cookbook>.
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.
22 This document covers a few of the ones we like best.
24 =head1 L<MooseX::AttributeHelpers>
26 The functionality of this MooseX module has been moved into Moose core.
27 See L<Moose::Meta::Attribute::Native>.
29 =head1 L<Moose::Autobox>
31 MooseX::AttributeHelpers, but turned inside out, Moose::Autobox provides
32 methods on both arrays/hashes/etc. but also references to them, using
33 Moose roles, allowing you do to things like:
37 $somebody_elses_object->orders->push($order);
39 Lexically scoped and not to everybody's taste, but very handy for sugaring
40 up other people's APIs and your own code.
42 =head1 L<MooseX::StrictConstructor>
44 By default, Moose lets you pass any old junk into a class's
45 constructor. If you load L<MooseX::StrictConstructor>, your class will
46 throw an error if it sees something it doesn't recognize;
51 use MooseX::StrictConstructor;
56 User->new( name => 'Bob', emali => 'bob@example.com' );
58 With L<MooseX::StrictConstructor>, that typo ("emali") will cause a
59 runtime error. With plain old Moose, the "emali" attribute would be
62 =head1 L<MooseX::Params::Validate>
64 We have high hopes for the future of L<MooseX::Method::Signatures> and
65 L<MooseX::Declare>. However, these modules, while used regularly in
66 production by some of the more insane members of the community, are
67 still marked alpha just in case backwards incompatible changes need to
70 If you don't want to risk that, for now we recommend the decidedly more
71 clunky (but also faster and simpler) L<MooseX::Params::Validate>. This
72 module lets you apply Moose types and coercions to any method
78 use MooseX::Params::Validate;
83 = validated_list( \@_, password => { isa => 'Str', required => 1 } );
88 =head1 L<MooseX::Getopt>
90 This is a role which adds a C<new_with_options> method to your
91 class. This is a constructor that takes the command line options and
92 uses them to populate attributes.
94 This makes writing a command-line application as a module trivially
100 with 'MooseX::Getopt';
116 Then in the script that gets run we have:
120 App::Foo->new_with_options->run;
122 From the command line, someone can execute the script:
124 foo@example> foo --input /path/to/input --output /path/to/output
126 =head1 L<MooseX::Singleton>
128 To be honest, using a singleton is just a way to have a magic global
129 variable in languages that don't actually have global variables.
131 In perl, you can just as easily use a global. However, if your
132 colleagues are Java-infected, they might prefer a singleton. Also, if
133 you have an existing class that I<isn't> a singleton but should be,
134 using L<MooseX::Singleton> is the easiest way to convert it.
138 use MooseX::Singleton; # instead of Moose
140 has 'cache_dir' => ( ... );
144 =head1 EXTENSIONS TO CONSIDER
146 There are literally dozens of other extensions on CPAN. This is a list
147 of extensions that you might find useful, but we're not quite ready to
150 =head2 L<MooseX::Declare>
152 Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
153 cool, but still new and experimental.
157 has 'name' => ( ... );
158 has 'email' => ( ... );
160 method login (Str $password) { ... }
163 =head2 L<MooseX::Types>
165 This extension helps you build a type library for your application. It
166 also lets you predeclare type names and use them as barewords.
168 use MooseX::Types -declare => ['PositiveInt'];
169 use MooseX::Types::Moose 'Int';
174 message { "Int is not larger than 0" };
176 One nice feature is that those bareword names are actually namespaced
177 in Moose's type registry, so multiple applications can use the same
178 bareword names, even if the type definitions differ.
180 =head2 L<MooseX::Types::Structured>
182 This extension builds on top of L<MooseX::Types> to let you declare
183 complex data structure types.
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);
190 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
193 => as Tuple[ Int, Int, Int, Optional[Int] ];
195 Of course, you could always use objects to represent these sorts of
198 =head2 L<MooseX::ClassAttribute>
200 This extension provides class attributes for Moose classes. The
201 declared class attributes are introspectable just like regular Moose
207 use MooseX::ClassAttribute;
209 has 'name' => ( ... );
211 class_has 'Cache' => ( ... );
213 Note however that this class attribute does I<not> inherit like a
214 L<Class::Data::Inheritable> or similar attribute - calling
216 $subclass->Cache($cache);
218 will set it for the superclass as well. Additionally, class data is usually
219 The Wrong Thing To Do in a strongly OO program since it makes testing a
220 lot harder - consider carefully whether you'd be better off with an object
221 that's passed around instead.
223 =head2 L<MooseX::Daemonize>
225 This is a role that provides a number of methods useful for creating a
226 daemon, including methods for starting and stopping, managing a PID
227 file, and signal handling.
229 =head2 L<MooseX::Role::Parameterized>
231 If you find yourself wanting a role that customizes itself for each
232 consumer, this is the tool for you. With this module, you can create a
233 role that accepts parameters and generates attributes, methods, etc. on
234 a customized basis for each consumer.
236 =head2 L<MooseX::POE>
238 This is a small wrapper that ties together a Moose class with
239 C<POE::Session>, and gives you an C<event> sugar function to declare
242 =head2 L<MooseX::FollowPBP>
244 Automatically names all accessors I<Perl Best Practices>-style,
245 "get_size" and "set_size".
247 =head2 L<MooseX::SemiAffordanceAccessor>
249 Automatically names all accessors with an explicit set and implicit
250 get, "size" and "set_size".
252 =head2 L<MooseX::NonMoose>
254 MooseX::NonMoose allows for easily subclassing non-Moose classes with Moose,
255 taking care of the annoying details connected with doing this, such as
256 setting up proper inheritance from Moose::Object and installing
257 (and inlining, at make_immutable time) a constructor that makes sure things
258 like BUILD methods are called.
262 Dave Rolsky E<lt>autarch@urth.orgE<gt>
264 =head1 COPYRIGHT AND LICENSE
266 Copyright 2009 by Infinity Interactive, Inc.
268 L<http://www.iinteractive.com>
270 This library is free software; you can redistribute it and/or modify
271 it under the same terms as Perl itself.