0.18 ... pretty much ready to go
[gitmo/Moose.git] / lib / Moose / Cookbook / FAQ.pod
CommitLineData
e67a0fca 1
2=pod
3
4=head1 NAME
5
6Moose::Cookbook::FAQ - Frequenty asked questions about Moose
7
8=head1 FREQUENTLY ASKED QUESTIONS
9
2a0f3bd3 10=head2 Module Stability
11
12=head3 Is Moose "production ready"?
13
734d1752 14Yes. I have two medium-to-large-ish web applications in
15production using Moose, they have been running without
16issue now for almost a year.
17
18At $work we are re-writing our core offering to use Moose,
19so it's continued development is assured.
20
21Several other people on #moose either have apps in production
22which use Moose, or are in the process of deploying sites
23which use Moose.
2a0f3bd3 24
25=head3 Is Moose's API stable?
26
27Yes and No. The external API, the one 90% of users will interact
28with, is B<very stable> and any changes B<will be 100% backwards
29compatible>. The introspection API is I<mostly> stable, I still
30reserve the right to tweak that if needed, but I will do my
31absolute best to maintain backwards comptability here as well.
32
734d1752 33=head3 I heard Moose is slow, is this true?
2a0f3bd3 34
35Again, this one is tricky, so Yes I<and> No.
36
37First let me say that I<nothing> in life is free, and that some
38Moose features do cost more than others. It is also the
39policy of Moose to B<only charge you for the features you use>,
40and to do our absolute best to not place any extra burdens on
d44714be 41the execution of your code for features you are not using. Of
42course using Moose itself does involve some overhead, but it
43is mostly compile time. At this point we do have some options
44available for getting the speed you need.
2a0f3bd3 45
734d1752 46Currently we have the option of making your classes immutable
47as a means of boosting speed. This will mean a larger compile
48time cost, but the runtime speed increase (especially in object
d44714be 49construction) is pretty signifigant. This is not very well
50documented yet, so please ask on the list of on #moose for more
51information.
52
53We are also discussing and experimenting with L<Module::Compile>,
54and the idea of compiling highly optimized C<.pmc> files. And
55we have also mapped out some core methods as canidates for
56conversion to XS.
2a0f3bd3 57
58=head3 When will Moose be 1.0 ready?
59
734d1752 60I had originally said it would be end of 2006, but various bits
61of $work kept me too busy. At this point, I think we are getting
62pretty close and I will likely declare 1.0 within the next few
63releases.
2a0f3bd3 64
734d1752 65When will that be? Hard to say really, but honestly, it is ready
66to use now, the difference between now and 1.0 will be pretty
67minimal.
2a0f3bd3 68
e67a0fca 69=head2 Constructors
70
71=head3 How do I write custom constructors with Moose?
72
73Ideally, you should never write your own C<new> method, and should
74use Moose's other features to handle your specific object construction
75needs. Here are a few scenarios, and the Moose way to solve them;
76
77If you need to call initializtion code post instance construction,
78then use the C<BUILD> method. This feature is taken directly from
79Perl 6. Every C<BUILD> method in your inheritence chain is called
80(in the correct order) immediately after the instance is constructed.
81This allows you to ensure that all your superclasses are initialized
82properly as well. This is the best approach to take (when possible)
83because it makes subclassing your class much easier.
84
85If you need to affect the constructor's parameters prior to the
86instance actually being constructed, you have a number of options.
87
88First, there are I<coercions> (See the L<Moose::Cookbook::Recipe5>
89for a complete example and explaination of coercions). With
90coercions it is possible to morph argument values into the correct
91expected types. This approach is the most flexible and robust, but
92does have a slightly higher learning curve.
93
94Second, using an C<around> method modifier on C<new> can be an
95effective way to affect the contents of C<@_> prior to letting
96Moose deal with it. This carries with it the extra burden for
97your subclasses, in that they have to be sure to explicitly
98call your C<new> and/or work around your C<new> to get to the
99version from L<Moose::Object>.
100
101The last approach is to use the standard Perl technique of calling
102the C<SUPER::new> within your own custom version of C<new>. This
103of course brings with it all the issues of the C<around> solution
104along with any issues C<SUPER::> might add as well.
105
106In short, try to use C<BUILD> and coercions, they are your best
107bets.
108
109=head3 How do I make non-Moose constuctors work with Moose?
110
111Moose provides it's own constructor, but it does it by making all
112Moose-based classes inherit from L<Moose::Object>. When inheriting
113from a non-Moose class, the inheritence chain to L<Moose::Object>
114is broken. The simplest way to fix this is to simply explicitly
115inherit from L<Moose::Object> yourself. However, this does not
116always fix the issue of a constructor. Here is a basic example of
117how this can be worked around:
118
119 package My::HTML::Template;
120 use Moose;
121
122 # explict inheritence
123 extends 'HTML::Template', 'Moose::Object';
124
125 # explicit constructor
126 sub new {
127 my $class = shift;
128 # call HTML::Template's constructor
129 my $obj = $class->SUPER::new(@_);
130 return $class->meta->new_object(
131 # pass in the constructed object
132 # using the special key __INSTANCE__
133 __INSTANCE__ => $obj, @_
134 );
135 }
136
137Of course this only works if both your Moose class, and the
138inherited non-Moose class use the same instance type (typically
139HASH refs).
140
141Other techniques can be used as well, such as creating the object
142using C<Moose::Object::new>, but calling the inherited non-Moose
143class's initializtion methods (if available).
144
145It is also entirely possible to just rely on HASH autovivification
146to create the slot's needed for Moose based attributes. Although
147this does somewhat restrict use of construction time attribute
148features.
149
150In short, there are several ways to go about this, it is best to
151evaluate each case based on the class you wish to extend, and the
152features you wish to employ. As always, both IRC and the mailing
153list are great ways to get help finding the best approach.
154
155=head2 Accessors
156
157=head3 How do I tell Moose to use get/set accessors?
158
159The easiest way to accomplish this is to use the C<reader> and
160C<writer> attribute options. Here is some example code:
161
162 has 'bar' => (
163 isa => 'Baz',
164 reader => 'get_bar',
165 writer => 'set_bar',
166 );
167
168Moose will still take advantage of type constraints, triggers, etc.
169when creating these methods.
170
171If you do not like this much typing, and wish it to be a default for
172your class. Please see L<Moose::Policy>, and more specifically the
173L<Moose::Policy::FollowPBP>. This will allow you to write this:
174
175 has 'bar' => (
176 isa => 'Baz',
177 is => 'rw',
178 );
179
180And have Moose create C<get_bar> and C<set_bar> instead of the usual
181C<bar>.
182
183NOTE: This B<cannot> be set globally in Moose, as this would break
184other classes which are built with Moose.
185
186=head3 How can I get Moose to inflate/deflate values in the accessor?
187
188Well, the first question to ask is if you actually need both inflate
189and deflate.
190
191If you only need to inflate, then I suggest using coercions. Here is
192some basic sample code for inflating a L<DateTime> object.
193
194 subtype 'DateTime'
195 => as 'Object'
196 => where { $_->isa('DateTime') };
197
198 coerce 'DateTime'
199 => from 'Str'
200 => via { DateTime::Format::MySQL->parse_datetime($_) };
201
202 has 'timestamp' => (is => 'rw', isa => 'DateTime', coerce => 1);
203
204This creates a custom subtype for L<DateTime> objects, then attaches
205a coercion to that subtype. The C<timestamp> attribute is then told
206to expect a C<DateTime> type, and to try and coerce it. When a C<Str>
207type is given to the C<timestamp> accessor, it will attempt to
208coerce the value into a C<DateTime> object using the code in found
209in the C<via> block.
210
211For a more detailed and complete example of coercions, see the
212L<Moose::Cookbook::Recipe5>.
213
214If you need to deflate your attribute, the current best practice is to
215add an C<around> modifier to your accessor. Here is some example code:
216
217 # a timestamp which stores as
218 # seconds from the epoch
219 has 'timestamp' => (is => 'rw', isa => 'Int');
220
221 around 'timestamp' => sub {
222 my $next = shift;
223 my ($self, $timestamp) = @_;
224 # assume we get a DateTime object ...
225 $next->($self, $timestamp->epoch);
226 };
227
228It is also possible to do deflation using coercion, but this tends
229to get quite complex and require many subtypes. An example of this
230is outside the scope of this document, ask on #moose or send a mail
231to the list.
232
233Still another option is to write a custom attribute metaclass, which
234is also outside the scope of this document, but I would be happy to
235explain it on #moose or the mailing list.
236
237=head2 Method Modfiers
238
239=head3 How can I affect the values in C<@_> using C<before>?
240
241You can't actually, C<before> only runs before the main method,
242and it cannot easily affect the execution of it. What you want is
243an C<around> method.
244
245=head3 Can I use C<before> to stop execution of a method?
246
247Yes, but only if you throw an exception. If this is too drastic a
248measure then I suggest using C<around> instead. The C<around> method
249modifier is the only modifier which can actually stop the execution
250of the main method. Here is an example:
251
252 around 'baz' => sub {
253 my $next = shift;
254 my ($self, %options) = @_;
255 if ($options{bar} eq 'foo') {
256 $next->($self, %options);
257 }
258 else {
259 return 'bar';
260 }
261 };
262
263By choosing not to call the C<$next> method, you can stop the
264execution of the main method.
265
266=head2 Type Constraints
267
268=head3 How can I have a custom error message for a type constraint?
269
270Use the C<message> option when building the subtype. Like so:
271
272 subtype 'NaturalLessThanTen'
273 => as 'Natural'
274 => where { $_ < 10 }
275 => message { "This number ($_) is not less than ten!" };
276
277This will be called when a value fails to pass the C<NaturalLessThanTen>
278constraint check.
279
734d1752 280=head3 Can I turn type constraint checking off?
281
282Not yet, but soon. This option will likely be coming in the next
283release.
284
e67a0fca 285=head1 AUTHOR
286
287Stevan Little E<lt>stevan@iinteractive.comE<gt>
288
289=head1 COPYRIGHT AND LICENSE
290
b77fdbed 291Copyright 2006, 2007 by Infinity Interactive, Inc.
e67a0fca 292
293L<http://www.iinteractive.com>
294
295This library is free software; you can redistribute it and/or modify
296it under the same terms as Perl itself.
297
298=cut