Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / lib / Moose / Manual / MooseX.pod
CommitLineData
daa0fd7d 1package Moose::Manual::MooseX;
2
3# ABSTRACT: Recommended Moose extensions
1d8f590f 4
daa0fd7d 5__END__
1d8f590f 6
daa0fd7d 7=pod
1d8f590f 8
9=head1 MooseX?
10
1d9599cb 11It's easy to extend and change Moose, and this is part of what makes
12Moose so powerful. You can use the MOP API to do things your own way,
1d8f590f 13add new features, and generally customize your Moose.
14
15Writing your own extensions does require a good understanding of the
1d9599cb 16meta-model. You can start learning about this with the
dab94063 17L<Moose::Manual::MOP> docs. There are also several extension recipes
1d9599cb 18in the L<Moose::Cookbook>.
1d8f590f 19
20Explaining how to write extensions is beyond the scope of this
21manual. Fortunately, lots of people have already written extensions
22and put them on CPAN for you.
23
24This document covers a few of the ones we like best.
25
d67ce58f 26=head1 L<MooseX::AttributeHelpers>
1d8f590f 27
e4a134ed 28The functionality of this MooseX module has been moved into Moose core.
29See L<Moose::Meta::Attribute::Native>.
1d8f590f 30
a434262f 31=head1 L<Moose::Autobox>
32
33MooseX::AttributeHelpers, but turned inside out, Moose::Autobox provides
34methods on both arrays/hashes/etc. but also references to them, using
35Moose roles, allowing you do to things like:
36
37 use Moose::Autobox;
38
39 $somebody_elses_object->orders->push($order);
40
41Lexically scoped and not to everybody's taste, but very handy for sugaring
42up other people's APIs and your own code.
43
d67ce58f 44=head1 L<MooseX::StrictConstructor>
1d8f590f 45
46By default, Moose lets you pass any old junk into a class's
0c39debe 47constructor. If you load L<MooseX::StrictConstructor>, your class will
1d8f590f 48throw an error if it sees something it doesn't recognize;
49
50 package User;
51
52 use Moose;
53 use MooseX::StrictConstructor;
54
55 has 'name';
56 has 'email';
57
58 User->new( name => 'Bob', emali => 'bob@example.com' );
59
0c39debe 60With L<MooseX::StrictConstructor>, that typo ("emali") will cause a
1d9599cb 61runtime error. With plain old Moose, the "emali" attribute would be
62silently ignored.
1d8f590f 63
d67ce58f 64=head1 L<MooseX::Params::Validate>
1d8f590f 65
0c39debe 66We have high hopes for the future of L<MooseX::Method::Signatures> and
a434262f 67L<MooseX::Declare>. However, these modules, while used regularly in
68production by some of the more insane members of the community, are
69still marked alpha just in case backwards incompatible changes need to
70be made.
71
72If you don't want to risk that, for now we recommend the decidedly more
0c39debe 73clunky (but also faster and simpler) L<MooseX::Params::Validate>. This
1d8f590f 74module lets you apply Moose types and coercions to any method
75arguments.
76
77 package User;
78
79 use Moose;
5a3fb5fc 80 use MooseX::Params::Validate;
1d8f590f 81
82 sub login {
83 my $self = shift;
84 my ($password)
1d9599cb 85 = validated_list( \@_, password => { isa => 'Str', required => 1 } );
1d8f590f 86
87 ...
88 }
89
d67ce58f 90=head1 L<MooseX::Getopt>
1d8f590f 91
92This is a role which adds a C<new_with_options> method to your
93class. This is a constructor that takes the command line options and
94uses them to populate attributes.
95
0f62a437 96This makes writing a command-line application as a module trivially
1d8f590f 97simple:
98
99 package App::Foo;
100
101 use Moose;
102 with 'MooseX::Getopt';
103
104 has 'input' => (
105 is => 'ro',
106 isa => 'Str',
107 required => 1
108 );
109
110 has 'output' => (
111 is => 'ro',
112 isa => 'Str',
113 required => 1
114 );
115
116 sub run { ... }
117
118Then in the script that gets run we have:
119
120 use App::Foo;
121
122 App::Foo->new_with_options->run;
123
1d9599cb 124From the command line, someone can execute the script:
125
126 foo@example> foo --input /path/to/input --output /path/to/output
127
d67ce58f 128=head1 L<MooseX::Singleton>
1d8f590f 129
a434262f 130To be honest, using a singleton is just a way to have a magic global
131variable in languages that don't actually have global variables.
132
e24c59ad 133In perl, you can just as easily use a global. However, if your
134colleagues are Java-infected, they might prefer a singleton. Also, if
135you have an existing class that I<isn't> a singleton but should be,
136using L<MooseX::Singleton> is the easiest way to convert it.
1d8f590f 137
138 package Config;
139
140 use MooseX::Singleton; # instead of Moose
141
142 has 'cache_dir' => ( ... );
143
144It's that simple.
145
146=head1 EXTENSIONS TO CONSIDER
147
1d9599cb 148There are literally dozens of other extensions on CPAN. This is a list
149of extensions that you might find useful, but we're not quite ready to
150endorse just yet.
1d8f590f 151
d67ce58f 152=head2 L<MooseX::Declare>
1d8f590f 153
154Extends Perl with Moose-based keywords using C<Devel::Declare>. Very
155cool, but still new and experimental.
156
157 class User {
158
159 has 'name' => ( ... );
160 has 'email' => ( ... );
161
162 method login (Str $password) { ... }
163 }
164
d67ce58f 165=head2 L<MooseX::Types>
1d8f590f 166
167This extension helps you build a type library for your application. It
6549b0d1 168also lets you predeclare type names and use them as barewords.
1d8f590f 169
5a3fb5fc 170 use MooseX::Types -declare => ['PositiveInt'];
1d8f590f 171 use MooseX::Types::Moose 'Int';
172
32a1dc89 173 subtype PositiveInt,
174 as Int,
175 where { $_ > 0 },
176 message { "Int is not larger than 0" };
1d8f590f 177
1d9599cb 178One nice feature is that those bareword names are actually namespaced
1d8f590f 179in Moose's type registry, so multiple applications can use the same
180bareword names, even if the type definitions differ.
181
d67ce58f 182=head2 L<MooseX::Types::Structured>
1d8f590f 183
0c39debe 184This extension builds on top of L<MooseX::Types> to let you declare
1d8f590f 185complex data structure types.
186
187 use MooseX::Types -declare => [ qw( Name Color ) ];
188 use MooseX::Types::Moose qw(Str Int);
189 use MooseX::Types::Structured qw(Dict Tuple Optional);
190
191 subtype Name
192 => as Dict[ first => Str, middle => Optional[Str], last => Str ];
193
194 subtype Color
195 => as Tuple[ Int, Int, Int, Optional[Int] ];
196
197Of course, you could always use objects to represent these sorts of
198things too.
199
d67ce58f 200=head2 L<MooseX::ClassAttribute>
1d8f590f 201
202This extension provides class attributes for Moose classes. The
203declared class attributes are introspectable just like regular Moose
204attributes.
205
206 package User;
207
208 use Moose;
209 use MooseX::ClassAttribute;
210
211 has 'name' => ( ... );
212
213 class_has 'Cache' => ( ... );
214
d6ed009f 215Note however that this class attribute does I<not> inherit like a
a434262f 216L<Class::Data::Inheritable> or similar attribute - calling
217
218 $subclass->Cache($cache);
219
220will set it for the superclass as well. Additionally, class data is usually
221The Wrong Thing To Do in a strongly OO program since it makes testing a
222lot harder - consider carefully whether you'd be better off with an object
223that's passed around instead.
224
d67ce58f 225=head2 L<MooseX::Daemonize>
1d8f590f 226
227This is a role that provides a number of methods useful for creating a
228daemon, including methods for starting and stopping, managing a PID
229file, and signal handling.
230
d67ce58f 231=head2 L<MooseX::Role::Parameterized>
1d8f590f 232
233If you find yourself wanting a role that customizes itself for each
234consumer, this is the tool for you. With this module, you can create a
5a3fb5fc 235role that accepts parameters and generates attributes, methods, etc. on
1d8f590f 236a customized basis for each consumer.
237
d67ce58f 238=head2 L<MooseX::POE>
1d8f590f 239
240This is a small wrapper that ties together a Moose class with
241C<POE::Session>, and gives you an C<event> sugar function to declare
242event handlers.
243
d67ce58f 244=head2 L<MooseX::FollowPBP>
ddf2636a 245
246Automatically names all accessors I<Perl Best Practices>-style,
1d9599cb 247"get_size" and "set_size".
ddf2636a 248
d67ce58f 249=head2 L<MooseX::SemiAffordanceAccessor>
ddf2636a 250
1d9599cb 251Automatically names all accessors with an explicit set and implicit
252get, "size" and "set_size".
ddf2636a 253
cc7dafef 254=head2 L<MooseX::NonMoose>
255
256MooseX::NonMoose allows for easily subclassing non-Moose classes with Moose,
257taking care of the annoying details connected with doing this, such as
258setting up proper inheritance from Moose::Object and installing
259(and inlining, at make_immutable time) a constructor that makes sure things
260like BUILD methods are called.
261
1d8f590f 262=cut