Quote subtype names here
[gitmo/Moose.git] / lib / Moose / Cookbook / FAQ.pod
index 69a972c..edf5926 100644 (file)
@@ -11,12 +11,12 @@ Moose::Cookbook::FAQ - Frequently asked questions about Moose
 
 =head3 Is Moose "production ready"?
 
-Yes. I have two medium-to-large-ish web applications in 
+Yes. I have several medium-to-large-ish web applications in 
 production using Moose, they have been running without 
-issue now for almost a year. 
+issue now for well over a year. 
 
-At $work we are re-writing our core offering to use Moose, 
-so it's continued development is assured. 
+At C<$work> we are re-writing our core offering to use Moose, 
+so its continued development is assured. 
 
 Several other people on #moose either have apps in production 
 which use Moose, or are in the process of deploying sites 
@@ -26,7 +26,7 @@ which use Moose.
 
 Yes and No. The external API, the one 90% of users will interact
 with, is B<very stable> and any changes B<will be 100% backwards 
-compatible>. The introspection API is I<mostly> stable, I still 
+compatible>. The introspection API is I<mostly> stable; I still 
 reserve the right to tweak that if needed, but I will do my 
 absolute best to maintain backwards compatibility here as well.
 
@@ -44,27 +44,20 @@ is mostly compile time. At this point we do have some options
 available for getting the speed you need. 
 
 Currently we have the option of making your classes immutable 
-as a means of boosting speed. This will mean a larger compile 
+as a means of boosting speed. This will mean a slightly larger compile 
 time cost, but the runtime speed increase (especially in object
 construction) is pretty significant. This is not very well 
-documented yet, so please ask on the list of on #moose for more
+documented yet, so please ask on the list or on #moose for more
 information.
 
-We are also discussing and experimenting with L<Module::Compile>, 
-and the idea of compiling highly optimized C<.pmc> files. And 
-we have also mapped out some core methods as candidates for 
-conversion to XS. 
+We are also discussing and experimenting with L<Module::Compile>,
+and the idea of compiling highly optimized C<.pmc> files. In
+addition, we have mapped out some core methods as candidates for
+conversion to XS.
 
-=head3 When will Moose be 1.0 ready?
+=head3 When will Moose 1.0 be ready?
 
-I had originally said it would be end of 2006, but various bits 
-of $work kept me too busy. At this point, I think we are getting 
-pretty close and I will likely declare 1.0 within the next few 
-releases. 
-
-When will that be? Hard to say really, but honestly, it is ready 
-to use now, the difference between now and 1.0 will be pretty 
-minimal.
+It is right now, I declared 0.18 to be "ready to use".
 
 =head2 Constructors
 
@@ -85,36 +78,35 @@ because it makes sub classing your class much easier.
 If you need to affect the constructor's parameters prior to the 
 instance actually being constructed, you have a number of options.
 
-First, there are I<coercions> (See the L<Moose::Cookbook::Recipe5> 
-for a complete example and explaination of coercions). With 
-coercions it is possible to morph argument values into the correct 
-expected types. This approach is the most flexible and robust, but 
-does have a slightly higher learning curve.
+To change the parameter processing as a whole, you can use
+the C<BUILDARGS> method. The default implementation accepts key/value
+pairs or a hash reference. You can override it to take positional args,
+or any other format
 
-Second, using an C<around> method modifier on C<new> can be an 
-effective way to affect the contents of C<@_> prior to letting 
-Moose deal with it. This carries with it the extra burden for 
-your subclasses, in that they have to be sure to explicitly 
-call your C<new> and/or work around your C<new> to get to the 
-version from L<Moose::Object>. 
+To change the handling of individual parameters, there are I<coercions>
+(See the L<Moose::Cookbook::Basics::Recipe5> for a complete example and
+explanation of coercions). With coercions it is possible to morph
+argument values into the correct expected types. This approach is the
+most flexible and robust, but does have a slightly higher learning
+curve.
 
-The last approach is to use the standard Perl technique of calling 
-the C<SUPER::new> within your own custom version of C<new>. This 
-of course brings with it all the issues of the C<around> solution 
-along with any issues C<SUPER::> might add as well.
+=head3 How do I make non-Moose constructors work with Moose? 
 
-In short, try to use C<BUILD> and coercions, they are your best 
-bets.
+Usually the correct approach to subclassing a non Moose class is
+delegation.  Moose makes this easy using the C<handles> keyword,
+coercions, and C<lazy_build>, so subclassing is often not the
+ideal route.
 
-=head3 How do I make non-Moose constructors work with Moose? 
+That said, the default Moose constructor is inherited from
+L<Moose::Object>. When inheriting from a non-Moose class, the
+inheritance chain to L<Moose::Object> is broken. The simplest way
+to fix this is to simply explicitly inherit from L<Moose::Object>
+yourself.
 
-Moose provides it's own constructor, but it does it by making all 
-Moose-based classes inherit from L<Moose::Object>. When inheriting
-from a non-Moose class, the inheritance chain to L<Moose::Object> 
-is broken. The simplest way to fix this is to simply explicitly 
-inherit from L<Moose::Object> yourself. However, this does not 
-always fix the issue of a constructor. Here is a basic example of 
-how this can be worked around:
+However, this does not always fix the issue of actually calling the Moose
+constructor. Fortunately L<Class::MOP::Class/new_object>, the low level
+constructor, accepts the special C<__INSTANCE__> parameter, allowing you to
+instantiate your Moose attributes:
 
   package My::HTML::Template;
   use Moose;
@@ -130,22 +122,25 @@ how this can be worked around:
       return $class->meta->new_object(
           # pass in the constructed object
           # using the special key __INSTANCE__
-          __INSTANCE__ => $obj, @_
+          __INSTANCE__ => $obj,
+          @_, # pass in the normal args
       );
   }
 
-Of course this only works if both your Moose class, and the 
+Of course, this only works if both your Moose class and the 
 inherited non-Moose class use the same instance type (typically 
-HASH refs). 
+HASH refs).
+
+Note that this doesn't call C<BUILDALL> automatically, you must do that
+yourself.
 
 Other techniques can be used as well, such as creating the object 
 using C<Moose::Object::new>, but calling the inherited non-Moose 
 class's initialization methods (if available). 
 
-It is also entirely possible to just rely on HASH autovivification 
-to create the slot's needed for Moose based attributes. Although 
-this does somewhat restrict use of construction time attribute 
-features.
+It is also entirely possible to just rely on HASH autovivification
+to create the slots needed for Moose based attributes, although this
+does restrict use of construction time attribute features somewhat.
 
 In short, there are several ways to go about this, it is best to 
 evaluate each case based on the class you wish to extend, and the 
@@ -168,19 +163,19 @@ C<writer> attribute options. Here is some example code:
 Moose will still take advantage of type constraints, triggers, etc. 
 when creating these methods. 
 
-If you do not like this much typing, and wish it to be a default for 
-your class. Please see L<Moose::Policy>, and more specifically the 
-L<Moose::Policy::FollowPBP>. This will allow you to write this:
+If you do not like this much typing, and wish it to be a default for
+your class, please see L<Moose::Policy>, and more specifically
+L<Moose::Policy::FollowPBP>. This will allow you to write:
 
   has 'bar' => (
       isa => 'Baz',
       is  => 'rw',
   );
 
-And have Moose create C<get_bar> and C<set_bar> instead of the usual 
-C<bar>.
+And have Moose create separate C<get_bar> and C<set_bar> methods
+instead of a single C<bar> method.
 
-NOTE: This B<cannot> be set globally in Moose, as this would break 
+NOTE: This B<cannot> be set globally in Moose, as that would break 
 other classes which are built with Moose.
 
 =head3 How can I get Moose to inflate/deflate values in the accessor?
@@ -189,7 +184,7 @@ Well, the first question to ask is if you actually need both inflate
 and deflate.
 
 If you only need to inflate, then I suggest using coercions. Here is 
-some basic sample code for inflating a L<DateTime> object. 
+some basic sample code for inflating a L<DateTime> object:
 
   subtype 'DateTime'
       => as 'Object'
@@ -203,13 +198,13 @@ some basic sample code for inflating a L<DateTime> object.
 
 This creates a custom subtype for L<DateTime> objects, then attaches 
 a coercion to that subtype. The C<timestamp> attribute is then told 
-to expect a C<DateTime> type, and to try and coerce it. When a C<Str>
+to expect a C<DateTime> type, and to try to coerce it. When a C<Str>
 type is given to the C<timestamp> accessor, it will attempt to 
 coerce the value into a C<DateTime> object using the code in found 
 in the C<via> block. 
 
-For a more detailed and complete example of coercions, see the 
-L<Moose::Cookbook::Recipe5>.
+For a more comprehensive example of using coercions, see the
+L<Moose::Cookbook::Basics::Recipe5>.
 
 If you need to deflate your attribute, the current best practice is to 
 add an C<around> modifier to your accessor. Here is some example code:
@@ -238,26 +233,24 @@ explain it on #moose or the mailing list.
 
 =head3 How can I affect the values in C<@_> using C<before>?
 
-You can't actually, C<before> only runs before the main method, 
-and it cannot easily affect the execution of it. What you want is 
+You can't, actually: C<before> only runs before the main method, 
+and it cannot easily affect the method's execution. What you want is 
 an C<around> method. 
 
 =head3 Can I use C<before> to stop execution of a method?
 
 Yes, but only if you throw an exception. If this is too drastic a 
 measure then I suggest using C<around> instead. The C<around> method 
-modifier is the only modifier which can actually stop the execution 
+modifier is the only modifier which can gracefully prevent execution 
 of the main method. Here is an example:
 
   around 'baz' => sub {
       my $next = shift;
       my ($self, %options) = @_;
-      if ($options{bar} eq 'foo') {
-          $next->($self, %options);
-      }
-      else {
-          return 'bar';
+      unless ($options->{bar} eq 'foo') {
+       return 'bar';
       }
+      $next->($self, %options);
   };
 
 By choosing not to call the C<$next> method, you can stop the 
@@ -267,7 +260,7 @@ execution of the main method.
 
 =head3 How can I have a custom error message for a type constraint?
 
-Use the C<message> option when building the subtype. Like so:
+Use the C<message> option when building the subtype, like so:
 
   subtype 'NaturalLessThanTen' 
       => as 'Natural'
@@ -277,22 +270,44 @@ Use the C<message> option when building the subtype. Like so:
 This will be called when a value fails to pass the C<NaturalLessThanTen>
 constraint check. 
 
-=head3 Can I turn type constraint checking off?
+=head3 Can I turn off type constraint checking?
 
 Not yet, but soon. This option will likely be coming in the next 
 release.
 
+=head2 Roles
+
+=head3 How do I get Moose to call BUILD in all my composed roles?
+
+See L<Moose::Cookbook::WTF> and specifically the B<How come BUILD 
+is not called for my composed roles?> question in the B<Roles> section.
+
+=head3 What are Traits, and how are they different to Roles?
+
+In Moose, a trait is almost exactly the same thing as a role, except
+that traits typically register themselves, which allows you to refer
+to them by a short name ("Big" vs "MyApp::Role::Big").
+
+In Moose-speak, a I<Role> is usually composed into a I<class> at
+compile time, whereas a I<Trait> is usually composed into an instance
+of a class at runtime to add or modify the behavior of B<just that
+instance>.
+
+Outside the context of Moose, traits and roles generally mean exactly the
+same thing. The original paper called them Traits, however Perl 6 will call
+them Roles.
+
 =head1 AUTHOR
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006, 2007 by Infinity Interactive, Inc.
+Copyright 2006-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
-=cut
\ No newline at end of file
+=cut