5 Moose::Manual::MooseX - Recommended Moose Extensions
9 One of the great things about Moose is that it is easy to extend and
10 override. You can use the meta-model 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 thiswith the
15 L<Moose::Manual::Introspection> docs. There are also several extensions
16 recipes 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 MooseX::AttributeHelpers
26 If you only look at one extension, it should be this one. The name
27 isn't the best, but what it does it provide the equivalent of
28 delegation for all of Perl's native data types, such as array
29 reference, hash references, numbers, strings, etc.
31 This lets you create I<much> cleaner and fluent APIs.
36 use MooseX::AttributeHelpers;
39 metaclass => 'Collection::Array',
42 default => sub { [] },
45 shift => 'next_order',
50 Instead of directly exposing an array reference, we have three
51 well-named, easy to use methods.
53 =head1 MooseX::StrictConstructor
55 By default, Moose lets you pass any old junk into a class's
56 constructor. If you load C<MooseX::StrictConstructor>, your class will
57 throw an error if it sees something it doesn't recognize;
62 use MooseX::StrictConstructor;
67 User->new( name => 'Bob', emali => 'bob@example.com' );
69 With C<MooseX::StrictConstructor>, that typo ("emali") will cause a
70 runtime error. Otherwise, the "emali" attribute would just be silently
73 =head1 MooseX::Params::Validate
75 We have high hopes for the future of C<MooseX::Method::Signatures> and
76 C<MooseX::Declare>. However, for now we recommend the decidely more
77 clunky (but also faster and simpler) C<MooseX::Params::Validate>. This
78 module lets you apply Moose types and coercions to any method
84 use MooseX::Params::Validate qw( validatep );
89 = validatep( \@_, password => { isa => 'Str', required => 1 } );
96 This is a role which adds a C<new_with_options> method to your
97 class. This is a constructor that takes the command line options and
98 uses them to populate attributes.
100 Thia makes writing a command-line application as a module trivially
106 with 'MooseX::Getopt';
122 Then in the script that gets run we have:
126 App::Foo->new_with_options->run;
128 =head1 MooseX::Singleton
130 To be honest, using a singleton is often a hack, but it sure is a
131 handy hack. C<MooseX::Singleton> lets you have a Moose class that's a
136 use MooseX::Singleton; # instead of Moose
138 has 'cache_dir' => ( ... );
142 =head1 MooseX::MetaDescription
144 This module lets you attach an arbitrary hash reference of metadata to
147 has 'image_name' => (
148 metaclass => 'MooseX::MetaDescription::Meta::Attribute',
152 html_display => 'Image',
156 =head1 EXTENSIONS TO CONSIDER
158 There are literally dozens of other extensions on CPAN. These are a
159 few to consider. We're not quite ready to recommend them outright,
160 though. There's a couple reasons for this. One, they may be very
161 specialized. Two, they may be immature. Three, they may not be quite
162 right yet. Four, we may not all agree they're such a great idea.
164 =head2 MooseX::Declare
166 Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
167 cool, but still new and experimental.
171 has 'name' => ( ... );
172 has 'email' => ( ... );
174 method login (Str $password) { ... }
179 This extension helps you build a type library for your application. It
180 also lets you pre-declare type names and use them as barewords.
182 use MooseX::Types -declare => ['PosInt'];
183 use MooseX::Types::Moose 'Int';
188 => message {"Int is not larger than 0"};
190 One nice feature is the those bareword names are actually namespaces
191 in Moose's type registry, so multiple applications can use the same
192 bareword names, even if the type definitions differ.
194 =head2 MooseX::Types::Structured
196 This extension builds on top of C<MooseX::Types> to let you declare
197 complex data structure types.
199 use MooseX::Types -declare => [ qw( Name Color ) ];
200 use MooseX::Types::Moose qw(Str Int);
201 use MooseX::Types::Structured qw(Dict Tuple Optional);
204 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
207 => as Tuple[ Int, Int, Int, Optional[Int] ];
209 Of course, you could always use objects to represent these sorts of
212 =head2 MooseX::ClassAttribute
214 This extension provides class attributes for Moose classes. The
215 declared class attributes are introspectable just like regular Moose
221 use MooseX::ClassAttribute;
223 has 'name' => ( ... );
225 class_has 'Cache' => ( ... );
227 =head2 MooseX::Daemonize
229 This is a role that provides a number of methods useful for creating a
230 daemon, including methods for starting and stopping, managing a PID
231 file, and signal handling.
233 =head2 MooseX::Role::Parameterized
235 If you find yourself wanting a role that customizes itself for each
236 consumer, this is the tool for you. With this module, you can create a
237 role that accepts parameters and generates attributes, methods, etc on
238 a customized basis for each consumer.
242 This is a small wrapper that ties together a Moose class with
243 C<POE::Session>, and gives you an C<event> sugar function to declare
246 =head2 MooseX::FollowPBP
248 Automatically names all accessors I<Perl Best Practices>-style,
249 "get_name" and "set_name".
251 =head2 MooseX::SemiAffordanceAccessor
253 Autoamtically names all accessors with an explicit set and implicit
254 get, "name" and "set_name".
258 Dave Rolsky E<lt>autarch@urth.orgE<gt>
260 =head1 COPYRIGHT AND LICENSE
262 Copyright 2008 by Infinity Interactive, Inc.
264 L<http://www.iinteractive.com>
266 This library is free software; you can redistribute it and/or modify
267 it under the same terms as Perl itself.