Merge branch 'master' of git.moose.perl.org:Moose
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Recipe1.pod
CommitLineData
c8d5f1e1 1
2=pod
3
4=head1 NAME
5
5f2aa10a 6Moose::Cookbook::Extending::Recipe1 - Moose extension overview
c8d5f1e1 7
8=head1 DESCRIPTION
9
0558aab4 10Moose provides several ways in which extensions can hook into Moose
11and change its behavior. Moose also has a lot of behavior that can be
12changed. This recipe will provide an overview of each extension method
13and give you some recommendations on what tools to use.
c8d5f1e1 14
15If you haven't yet read the recipes on metaclasses, go read those
0558aab4 16first. You can't write Moose extensions without understanding the
17metaclasses, and those recipes also demonstrate some basic extension
18mechanisms, such as metaclass subclasses and traits.
c8d5f1e1 19
20=head2 Playing Nice With Others
21
22One of the goals of this overview is to help you build extensions that
23cooperate well with other extensions. This is especially important if
24you plan to release your extension to CPAN.
25
26Moose comes with several modules that exist to help your write
27cooperative extensions. These are L<Moose::Exporter> and
0558aab4 28L<Moose::Util::MetaRole>. By using these two modules, you will ensure
29that your extension works with both the Moose core features and any
30other CPAN extension using those modules.
c8d5f1e1 31
32=head1 PARTS OF Moose YOU CAN EXTEND
33
0558aab4 34The types of things you might want to do in Moose extensions fall into
35a few broad categories.
c8d5f1e1 36
37=head2 Metaclass Extensions
38
39One way of extending Moose is by extending one or more Moose
40metaclasses. For example, in L<Moose::Cookbook::Meta::Recipe4> we saw
41a metaclass subclass that added a C<table> attribute to the
42metaclass. If you were writing an ORM, this would be a logical
43extension.
44
45Many of the Moose extensions on CPAN work by providing an attribute
0558aab4 46metaclass extension. For example, the L<MooseX::AttributeHelpers>
6549b0d1 47distribution provides a new attribute metaclass that lets you delegate
c8d5f1e1 48behavior to a non-object attribute (a hashref or simple number).
49
50A metaclass extension can be packaged as a subclass or a
51role/trait. If you can, we recommend using traits instead of
38f0beac 52subclasses, since it's much easier to combine disparate traits than it
0558aab4 53is to combine a bunch of subclasses.
c8d5f1e1 54
55When your extensions are implemented as roles, you can apply them with
56the L<Moose::Util::MetaRole> module.
57
0558aab4 58=head2 Providing Sugar Functions
c8d5f1e1 59
60As part of a metaclass extension, you may also want to provide some
0558aab4 61sugar functions, just like L<Moose.pm|Moose> does. Moose provides a
62helper module called L<Moose::Exporter> that makes this much
63simpler. We will be use L<Moose::Exporter> in several of the extension
64recipes.
c8d5f1e1 65
66=head2 Object Class Extensions
67
0558aab4 68Another common Moose extension technique is to change the default
69object class's behavior. For example, the L<MooseX::Singleton>
70extension changes the behavior of your objects so that they are
71singletons. The L<MooseX::StrictConstructor> extension makes the
72constructor reject arguments which don't match its attributes.
c8d5f1e1 73
0558aab4 74Object class extensions often include metaclass extensions as well. In
c8d5f1e1 75particular, if you want your object extension to work when a class is
76made immutable, you may need to extend some or all of the
0558aab4 77L<Moose::Meta::Instance>, L<Moose::Meta::Method::Constructor>, and
78L<Moose::Meta::Method::Destructor> objects.
c8d5f1e1 79
80The L<Moose::Util::MetaRole> module lets you apply roles to the base
81object class, as well as the meta classes just mentioned.
82
83=head2 Providing a Role
84
85Some extensions come in the form of a role for you to consume. The
0558aab4 86L<MooseX::Object::Pluggable> extension is a great example of this. In
c8d5f1e1 87fact, despite the C<MooseX> name, it does not actually change anything
88about Moose's behavior. Instead, it is just a role that an object
89which wants to be pluggable can consume.
90
91If you are implementing this sort of extension, you don't need to do
92anything special. You simply create a role and document that it should
93be used via the normal C<with> sugar:
94
0558aab4 95 package MyApp::User;
c8d5f1e1 96
97 use Moose;
98
99 with 'MooseX::My::Role';
100
101=head2 New Types
102
103Another common Moose extension is a new type for the Moose type
104system. In this case, you simply create a type in your module. When
105people load your module, the type is created, and they can refer to it
0558aab4 106by name after that. The L<MooseX::Types::URI> and
107L<MooseX::Types::DateTime> distributions are two good examples of how
108this works. These both build on top of the L<MooseX::Types> extension.
c8d5f1e1 109
110=head1 ROLES VS TRAITS VS SUBCLASSES
111
713eb831 112It is important to understand that B<roles and traits are the same thing>. A
113trait is simply a role applied to a metaclass. The only thing that may
114distinguish the two is that a trait can be packaged in a way that lets Moose
115resolve a short name to a class name. In other words, with a trait, the caller
116can refer to it by a short name like "Big", and Moose will resolve it to a
117class like C<MooseX::Embiggen::Meta::Attribute::Role::Big>.
c8d5f1e1 118
119See L<Moose::Cookbook::Meta::Recipe3> and
120L<Moose::Cookbook::Meta::Recipe5> for examples of traits in action. In
121particular, both of these recipes demonstrate the trait resolution
122mechanism.
123
124Implementing an extension as a (set of) metaclass or base object
125role(s) will make your extension more cooperative. It is hard for an
126end-user to effectively combine together multiple metaclass
0558aab4 127subclasses, but it is very easy to combine roles.
c8d5f1e1 128
129=head1 USING YOUR EXTENSION
130
131There are a number of ways in which an extension can be applied. In
132some cases you can provide multiple ways of consuming your extension.
133
134=head2 Extensions as Metaclass Traits
135
136If your extension is available as a trait, you can ask end users to
137simply specify it in a list of traits. Currently, this only works for
0558aab4 138(class) metaclass and attribute metaclass traits:
c8d5f1e1 139
140 use Moose -traits => [ 'Big', 'Blue' ];
141
6a7e3999 142 has 'animal' => (
143 traits => [ 'Big', 'Blue' ],
144 ...
145 );
c8d5f1e1 146
147If your extension applies to any other metaclass, or the object base
148class, you cannot use the trait mechanism.
149
150The benefit of the trait mechanism is that is very easy to see where a
151trait is applied in the code, and consumers have fine-grained control
152over what the trait applies to. This is especially true for attribute
153traits, where you can apply the trait to just one attribute in a
154class.
155
156=head2 Extensions as Metaclass (and Base Object) Subclasses
157
158Moose does not provide any simple APIs for consumers to use a subclass
4223567e 159extension, except for attribute metaclasses. The attribute declaration
16fb3624 160options include a C<metaclass> option a consumer of your extension can
161use to specify your subclass.
c8d5f1e1 162
163This is one reason why implementing an extension as a subclass can be
164a poor choice. However, you can force the use of certain subclasses at
165import time by calling C<< Moose->init_meta >> for the caller, and
166providing an alternate metaclass or base object class.
167
0558aab4 168If you do want to do this, you should look at using L<Moose::Exporter>
169to re-export the L<Moose.pm|Moose> sugar function. With
170L<Moose::Exporter>, if your exporting class has an C<init_meta>
c8d5f1e1 171method, L<Moose::Exporter> makes sure that this C<init_meta> method
172gets called when your class is imported.
173
174Then in your C<init_meta> you can arrange for the caller to use your
175subclasses:
176
177 package MooseX::Embiggen;
178
179 use Moose ();
180 use Moose::Exporter;
181
182 use MooseX::Embiggen::Meta::Class;
183 use MooseX::Embiggen::Object;
184
185 Moose::Exporter->setup_import_methods( also => 'Moose' );
186
187 sub init_meta {
6a7e3999 188 shift; # just your package name
c8d5f1e1 189 my %options = @_;
190
191 return Moose->init_meta(
192 for_class => $options{for_class},
193 metaclass => 'MooseX::Embiggen::Meta::Class',
194 base_class => 'MooseX::Embiggen::Object',
195 );
196 }
197
a8de959b 198NOTE: Make sure that your C<init_meta> returns the metaclass object, just as
199C<< Moose->init_meta >> does.
200
c8d5f1e1 201=head2 Extensions as Metaclass (and Base Object) Roles
202
203Implementing your extensions as metaclass roles makes your extensions
0558aab4 204easy to apply, and cooperative with other role-based extensions for
205metaclasses.
c8d5f1e1 206
207Just as with a subclass, you will probably want to package your
208extensions for consumption with a single module that uses
209L<Moose::Exporter>. However, in this case, you will use
210L<Moose::Util::MetaRole> to apply all of your roles. The advantage of
211using this module is that I<it preserves any subclassing or roles
0558aab4 212already applied to the user's metaclasses>. This means that your
c8d5f1e1 213extension is cooperative I<by default>, and consumers of your
214extension can easily use it with other role-based extensions.
215
216 package MooseX::Embiggen;
217
218 use Moose ();
219 use Moose::Exporter;
220 use Moose::Util::MetaRole;
221
222 use MooseX::Embiggen::Role::Meta::Class;
223 use MooseX::Embiggen::Role::Meta::Attribute;
6a7e3999 224 use MooseX::Embiggen::Role::Meta::Method::Constructor;
c8d5f1e1 225 use MooseX::Embiggen::Role::Object;
226
227 Moose::Exporter->setup_import_methods( also => 'Moose' );
228
229 sub init_meta {
6a7e3999 230 shift; # just your package name
c8d5f1e1 231 my %options = @_;
232
233 Moose->init_meta(%options);
234
235 my $meta = Moose::Util::MetaRole::apply_metaclass_roles(
236 for_class => $options{for_class},
237 metaclass_roles => ['MooseX::Embiggen::Role::Meta::Class'],
238 attribute_metaclass_roles =>
239 ['MooseX::Embiggen::Role::Meta::Attribute'],
240 constructor_class_roles =>
241 ['MooseX::Embiggen::Role::Meta::Method::Constructor'],
242 );
243
244 Moose::Util::MetaRole::apply_base_class_roles(
245 for_class => $options{for_class},
246 roles => ['MooseX::Embiggen::Role::Object'],
247 );
248
249 return $meta;
250 }
251
0558aab4 252As you can see from this example, you can use L<Moose::Util::MetaRole>
c8d5f1e1 253to apply roles to any metaclass, as well as the base object class. If
254some other extension has already applied its own roles, they will be
255preserved when your extension applies its roles, and vice versa.
256
257=head2 Providing Sugar
258
0558aab4 259With L<Moose::Exporter>, you can also export your own sugar functions,
260as well as those from other modules:
c8d5f1e1 261
262 package MooseX::Embiggen;
263
264 use Moose ();
265 use Moose::Exporter;
266
267 Moose::Exporter->setup_import_methods(
268 with_caller => ['embiggen'],
269 also => 'Moose',
270 );
271
272 sub init_meta { ... }
273
274 sub embiggen {
275 my $caller = shift;
276 $caller->meta()->embiggen(@_);
277 }
278
279And then the consumer of your extension can use your C<embiggen> sub:
280
281 package Consumer;
282
283 use MooseX::Embiggen;
284
285 extends 'Thing';
286
287 embiggen ...;
288
289This can be combined with metaclass and base class roles quite easily.
290
0558aab4 291=head1 LEGACY EXTENSION MECHANISMS
c8d5f1e1 292
293Before the existence of L<Moose::Exporter> and
294L<Moose::Util::MetaRole>, there were a number of other ways to extend
295Moose. In general, these methods were less cooperative, and only
296worked well with a single extension.
297
0558aab4 298These methods include L<metaclass.pm|metaclass>, L<Moose::Policy>
299(which uses L<metaclass.pm|metaclass> under the hood), and various
300hacks to do what L<Moose::Exporter> does. Please do not use these for
301your own extensions.
c8d5f1e1 302
303Note that if you write a cooperative extension, it should cooperate
304with older extensions, though older extensions generally do not
8745c929 305cooperate with each other.
c8d5f1e1 306
307=head1 CONCLUSION
308
309If you can write your extension as one or more metaclass and base
310object roles, please consider doing so. Make sure to read the docs for
311L<Moose::Exporter> and L<Moose::Util::MetaRole> as well.
312
313=head1 AUTHOR
314
315Dave Rolsky E<lt>autarch@urth.orgE<gt>
316
317=head1 COPYRIGHT AND LICENSE
318
2840a3b2 319Copyright 2009 by Infinity Interactive, Inc.
c8d5f1e1 320
321L<http://www.iinteractive.com>
322
323This library is free software; you can redistribute it and/or modify
324it under the same terms as Perl itself.
325
326=cut