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 EXTENSIONS TO CONSIDER
144 There are literally dozens of other extensions on CPAN. These are a
145 few to consider. We're not quite ready to recommend them outright,
146 though. There's a couple reasons for this. One, they may be very
147 specialized. Two, they may be immature. Three, they may not be quite
148 right yet. Four, we may not all agree they're such a great idea.
150 =head2 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) { ... }
165 This extension helps you build a type library for your application. It
166 also lets you pre-declare type names and use them as barewords.
168 use MooseX::Types -declare => ['PosInt'];
169 use MooseX::Types::Moose 'Int';
174 => message {"Int is not larger than 0"};
176 One nice feature is the those bareword names are actually namespaces
177 in Moose's type registry, so multiple applications can use the same
178 bareword names, even if the type definitions differ.
180 =head2 MooseX::Types::Structured
182 This extension builds on top of C<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 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 =head2 MooseX::Daemonize
215 This is a role that provides a number of methods useful for creating a
216 daemon, including methods for starting and stopping, managing a PID
217 file, and signal handling.
219 =head2 MooseX::Role::Parameterized
221 If you find yourself wanting a role that customizes itself for each
222 consumer, this is the tool for you. With this module, you can create a
223 role that accepts parameters and generates attributes, methods, etc on
224 a customized basis for each consumer.
228 This is a small wrapper that ties together a Moose class with
229 C<POE::Session>, and gives you an C<event> sugar function to declare
232 =head2 MooseX::FollowPBP
234 Automatically names all accessors I<Perl Best Practices>-style,
235 "get_name" and "set_name".
237 =head2 MooseX::SemiAffordanceAccessor
239 Autoamtically names all accessors with an explicit set and implicit
240 get, "name" and "set_name".
244 Dave Rolsky E<lt>autarch@urth.orgE<gt>
246 =head1 COPYRIGHT AND LICENSE
248 Copyright 2008 by Infinity Interactive, Inc.
250 L<http://www.iinteractive.com>
252 This library is free software; you can redistribute it and/or modify
253 it under the same terms as Perl itself.