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 | |
2222bdc4 |
14 | Yes! Many sites with household names are using Moose to build |
15 | high-traffic services. Countless others are using Moose in |
16 | production. |
734d1752 |
17 | |
2222bdc4 |
18 | As of this writing, Moose is a dependency of several hundred CPAN |
19 | modules. L<http://cpants.perl.org/dist/used_by/Moose> |
2a0f3bd3 |
20 | |
21 | =head3 Is Moose's API stable? |
22 | |
c698114d |
23 | Yes. The sugary API, the one 95% of users will interact with, is |
24 | B<very stable>. Any changes will be B<100% backwards compatible>. |
25 | |
26 | The meta API is less set in stone. We reserve the right to tweak |
27 | parts of it to improve efficiency or consistency. This will not be |
28 | done lightly. We do perform deprecation cycles. We I<really> |
29 | do not like making ourselves look bad by breaking your code. |
30 | Submitting test cases is the best way to ensure that your code is not |
31 | inadvertantly broken by refactoring. |
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 | |
c32ff715 |
37 | Firstly, I<nothing> in life is free, and some Moose features |
38 | do cost more than others. It is also the policy of Moose to |
39 | B<only charge you for the features you use>, and to do our |
40 | absolute best to not place any extra burdens on the execution |
41 | of your code for features you are not using. Of course using |
42 | Moose itself does involve some overhead, but it is mostly |
43 | compile time. At this point we do have some options available |
44 | for getting the speed you need. |
45 | |
46 | Currently we provide the option of making your classes immutable |
d03bd989 |
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 |
c32ff715 |
49 | construction) is pretty significant. |
50 | |
51 | We are regularly converting the hotspots of L<Class::MOP> to XS. |
52 | Florian Ragwitz and Yuval Kogman are currently working on a way |
53 | to compile your accessors and instances directly into C, so that |
54 | everyone can enjoy blazing fast OO. |
2a0f3bd3 |
55 | |
807f6b7c |
56 | =head3 When will Moose 1.0 be ready? |
2a0f3bd3 |
57 | |
37a9448b |
58 | Moose is ready now! Stevan Little declared 0.18, released in March 2007, |
afc402d9 |
59 | to be "ready to use". |
2a0f3bd3 |
60 | |
e67a0fca |
61 | =head2 Constructors |
62 | |
63 | =head3 How do I write custom constructors with Moose? |
64 | |
65 | Ideally, you should never write your own C<new> method, and should |
66 | use Moose's other features to handle your specific object construction |
67 | needs. Here are a few scenarios, and the Moose way to solve them; |
68 | |
d03bd989 |
69 | If you need to call initialization code post instance construction, |
70 | then use the C<BUILD> method. This feature is taken directly from |
71 | Perl 6. Every C<BUILD> method in your inheritance chain is called |
72 | (in the correct order) immediately after the instance is constructed. |
73 | This allows you to ensure that all your superclasses are initialized |
e67a0fca |
74 | properly as well. This is the best approach to take (when possible) |
dab94063 |
75 | because it makes subclassing your class much easier. |
e67a0fca |
76 | |
d03bd989 |
77 | If you need to affect the constructor's parameters prior to the |
e67a0fca |
78 | instance actually being constructed, you have a number of options. |
79 | |
ce21ecc5 |
80 | To change the parameter processing as a whole, you can use |
81 | the C<BUILDARGS> method. The default implementation accepts key/value |
82 | pairs or a hash reference. You can override it to take positional args, |
83 | or any other format |
84 | |
85 | To change the handling of individual parameters, there are I<coercions> |
5cfe3805 |
86 | (See the L<Moose::Cookbook::Basics::Recipe5> for a complete example and |
a8de15f8 |
87 | explanation of coercions). With coercions it is possible to morph |
ce21ecc5 |
88 | argument values into the correct expected types. This approach is the |
89 | most flexible and robust, but does have a slightly higher learning |
90 | curve. |
e67a0fca |
91 | |
d03bd989 |
92 | =head3 How do I make non-Moose constructors work with Moose? |
e67a0fca |
93 | |
6c17df8f |
94 | Usually the correct approach to subclassing a non-Moose class is |
e760b278 |
95 | delegation. Moose makes this easy using the C<handles> keyword, |
96 | coercions, and C<lazy_build>, so subclassing is often not the |
97 | ideal route. |
98 | |
fe4975b3 |
99 | That said, if you really need to inherit from a non-Moose class, see |
100 | L<Moose::Cookbook::Basics::Recipe12> for an example of how to do it. |
e67a0fca |
101 | |
102 | =head2 Accessors |
103 | |
104 | =head3 How do I tell Moose to use get/set accessors? |
105 | |
d03bd989 |
106 | The easiest way to accomplish this is to use the C<reader> and |
6c17df8f |
107 | C<writer> attribute options: |
e67a0fca |
108 | |
109 | has 'bar' => ( |
110 | isa => 'Baz', |
d03bd989 |
111 | reader => 'get_bar', |
e67a0fca |
112 | writer => 'set_bar', |
113 | ); |
114 | |
d03bd989 |
115 | Moose will still take advantage of type constraints, triggers, etc. |
116 | when creating these methods. |
e67a0fca |
117 | |
1a835594 |
118 | If you do not like this much typing, and wish it to be a default for your |
6c17df8f |
119 | classes, please see L<MooseX::FollowPBP>. This extension will allow you to |
120 | write: |
e67a0fca |
121 | |
122 | has 'bar' => ( |
123 | isa => 'Baz', |
124 | is => 'rw', |
125 | ); |
126 | |
6c17df8f |
127 | Moose will create separate C<get_bar> and C<set_bar> methods |
807f6b7c |
128 | instead of a single C<bar> method. |
e67a0fca |
129 | |
d03bd989 |
130 | NOTE: This B<cannot> be set globally in Moose, as that would break |
6c17df8f |
131 | other classes which are built with Moose. You can still save on typing |
132 | by defining a new L<MyApp::Moose> that exports Moose's sugar and then |
133 | turns on L<MooseX::FollowPBP>. See L<Moose::Cookbook::Extending::Recipe4>. |
e67a0fca |
134 | |
6c17df8f |
135 | =head3 How can I inflate/deflate values in accessors? |
e67a0fca |
136 | |
d03bd989 |
137 | Well, the first question to ask is if you actually need both inflate |
e67a0fca |
138 | and deflate. |
139 | |
8b870d0e |
140 | If you only need to inflate, then we suggest using coercions. Here is |
807f6b7c |
141 | some basic sample code for inflating a L<DateTime> object: |
e67a0fca |
142 | |
6c17df8f |
143 | class_type 'DateTime'; |
d03bd989 |
144 | |
e67a0fca |
145 | coerce 'DateTime' |
146 | => from 'Str' |
147 | => via { DateTime::Format::MySQL->parse_datetime($_) }; |
d03bd989 |
148 | |
e67a0fca |
149 | has 'timestamp' => (is => 'rw', isa => 'DateTime', coerce => 1); |
150 | |
6c17df8f |
151 | This creates a custom type for L<DateTime> objects, then attaches |
152 | a coercion to that type. The C<timestamp> attribute is then told |
807f6b7c |
153 | to expect a C<DateTime> type, and to try to coerce it. When a C<Str> |
d03bd989 |
154 | type is given to the C<timestamp> accessor, it will attempt to |
155 | coerce the value into a C<DateTime> object using the code in found |
156 | in the C<via> block. |
e67a0fca |
157 | |
807f6b7c |
158 | For a more comprehensive example of using coercions, see the |
5cfe3805 |
159 | L<Moose::Cookbook::Basics::Recipe5>. |
e67a0fca |
160 | |
6c17df8f |
161 | If you need to deflate your attribute's value, the current best practice |
162 | is to add an C<around> modifier to your accessor: |
e67a0fca |
163 | |
d03bd989 |
164 | # a timestamp which stores as |
e67a0fca |
165 | # seconds from the epoch |
166 | has 'timestamp' => (is => 'rw', isa => 'Int'); |
d03bd989 |
167 | |
e67a0fca |
168 | around 'timestamp' => sub { |
169 | my $next = shift; |
170 | my ($self, $timestamp) = @_; |
171 | # assume we get a DateTime object ... |
172 | $next->($self, $timestamp->epoch); |
173 | }; |
174 | |
d03bd989 |
175 | It is also possible to do deflation using coercion, but this tends |
176 | to get quite complex and require many subtypes. An example of this |
177 | is outside the scope of this document, ask on #moose or send a mail |
e67a0fca |
178 | to the list. |
179 | |
d03bd989 |
180 | Still another option is to write a custom attribute metaclass, which |
8b870d0e |
181 | is also outside the scope of this document, but we would be happy to |
e67a0fca |
182 | explain it on #moose or the mailing list. |
183 | |
4711f5f7 |
184 | =head2 Method Modifiers |
e67a0fca |
185 | |
186 | =head3 How can I affect the values in C<@_> using C<before>? |
187 | |
d03bd989 |
188 | You can't, actually: C<before> only runs before the main method, |
e30e57c6 |
189 | and it cannot easily affect the method's execution. |
190 | |
191 | You similarly can't use C<after> to affect the return value of a |
192 | method. |
193 | |
194 | We limit C<before> and C<after> because this lets you write more |
195 | concise code. You do not have to worry about passing C<@_> to the |
6c17df8f |
196 | original method, or forwarding its return value (being careful to |
197 | preserve context). |
e30e57c6 |
198 | |
6c17df8f |
199 | The C<around> method modifier has neither of these limitations, but |
200 | is a little more verbose. |
e67a0fca |
201 | |
202 | =head3 Can I use C<before> to stop execution of a method? |
203 | |
d03bd989 |
204 | Yes, but only if you throw an exception. If this is too drastic a |
8b870d0e |
205 | measure then we suggest using C<around> instead. The C<around> method |
d03bd989 |
206 | modifier is the only modifier which can gracefully prevent execution |
e67a0fca |
207 | of the main method. Here is an example: |
208 | |
6c17df8f |
209 | around 'baz' => sub { |
210 | my $next = shift; |
211 | my ($self, %options) = @_; |
212 | unless ($options->{bar} eq 'foo') { |
213 | return 'bar'; |
214 | } |
215 | $next->($self, %options); |
216 | }; |
e67a0fca |
217 | |
d03bd989 |
218 | By choosing not to call the C<$next> method, you can stop the |
e67a0fca |
219 | execution of the main method. |
220 | |
221 | =head2 Type Constraints |
222 | |
6c17df8f |
223 | =head3 How can I provide a custom error message for a type constraint? |
e67a0fca |
224 | |
6c17df8f |
225 | Use the C<message> option when building the subtype: |
e67a0fca |
226 | |
d03bd989 |
227 | subtype 'NaturalLessThanTen' |
e67a0fca |
228 | => as 'Natural' |
229 | => where { $_ < 10 } |
230 | => message { "This number ($_) is not less than ten!" }; |
231 | |
6c17df8f |
232 | This C<message> block will be called when a value fails to pass the |
233 | C<NaturalLessThanTen> constraint check. |
e67a0fca |
234 | |
807f6b7c |
235 | =head3 Can I turn off type constraint checking? |
734d1752 |
236 | |
6c17df8f |
237 | Not yet. This option will likely come in a future release. |
734d1752 |
238 | |
b36c8076 |
239 | =head2 Roles |
240 | |
241 | =head3 How do I get Moose to call BUILD in all my composed roles? |
242 | |
6c17df8f |
243 | See L<Moose::Cookbook::WTF> and specifically the |
244 | B<Why is BUILD not called for my composed roles?> question in the |
245 | B<Roles> section. |
b36c8076 |
246 | |
dab94063 |
247 | =head3 What are Traits, and how are they different from Roles? |
a8de15f8 |
248 | |
457ad5fc |
249 | In Moose, a trait is almost exactly the same thing as a role, except |
250 | that traits typically register themselves, which allows you to refer |
251 | to them by a short name ("Big" vs "MyApp::Role::Big"). |
252 | |
253 | In Moose-speak, a I<Role> is usually composed into a I<class> at |
254 | compile time, whereas a I<Trait> is usually composed into an instance |
6c17df8f |
255 | of a class at runtime to add or modify the behavior of |
256 | B<just that instance>. |
a8de15f8 |
257 | |
258 | Outside the context of Moose, traits and roles generally mean exactly the |
259 | same thing. The original paper called them Traits, however Perl 6 will call |
260 | them Roles. |
261 | |
e67a0fca |
262 | =head1 AUTHOR |
263 | |
264 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
265 | |
266 | =head1 COPYRIGHT AND LICENSE |
267 | |
2840a3b2 |
268 | Copyright 2006-2009 by Infinity Interactive, Inc. |
e67a0fca |
269 | |
270 | L<http://www.iinteractive.com> |
271 | |
272 | This library is free software; you can redistribute it and/or modify |
273 | it under the same terms as Perl itself. |
274 | |
e760b278 |
275 | =cut |