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