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