07f3a8ece6169464aa700857630019a5062b7b07
[gitmo/Moose.git] / lib / Moose / Cookbook / WTF.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::WTF - For when things go wrong with Moose
7
8 =head1 COMMON PROBLEMS
9
10 =head2 Speed
11
12 =head3 Why is my code taking so long to load?
13
14 Moose has a fairly heavy compile time burden, which it 
15 inherits from Class::MOP. If load/compile time is a 
16 concern for your application, Moose may not be the 
17 right tool for you. 
18
19 Although, you should note that we are exploring the 
20 use of L<Module::Compile> to try and reduce this problem, 
21 but nothing is ready yet.
22
23 =head3 Why are my objects taking so long to construct?
24
25 Moose uses a lot of introspection when constructing an 
26 instance, and introspection can be slow. This problem 
27 can be solved by making your class immutable. This can 
28 be done with the following code:
29
30   MyClass->meta->make_immutable();
31
32 Moose will then memoize a number of meta-level methods
33 and inline a constructor for you. For more information 
34 on this see the L<Constructors> section below and in the 
35 L<Moose::Cookbook::FAQ>.
36
37 =head2 Constructors & Immutability
38
39 =head3 I made my class immutable, but C<new> it is still slow!
40
41 Do you have a custom C<new> method in your class? Moose 
42 will not overwrite your custom C<new> method, you would 
43 probably do better to try and convert this to use the 
44 C<BUILD> method or possibly set C<default> values in 
45 the attribute declaration. 
46
47 =head3 I made my class immutable, and now my (before | after | around) C<new> is not being called?
48
49 Making a I<before>, I<after> or I<around> wrap around the 
50 C<new> method, will actually create a C<new> method within 
51 your class. This will prevent Moose from creating one itself
52 when you make the class immutable. 
53
54 =head2 Accessors
55
56 =head3 I created an attribute, where are my accessors?
57
58 Accessors are B<not> created implicitly, you B<must> ask Moose 
59 to create them for you. My guess is that you have this:
60
61   has 'foo' => (isa => 'Bar');
62
63 when what you really want to say is:
64
65   has 'foo' => (isa => 'Bar', is => 'rw');
66
67 The reason this is so, is because it is a perfectly valid use 
68 case to I<not> have an accessor. The simplest one is that you 
69 want to write your own. If Moose created on automatically, then
70 because of the order in which classes are constructed, Moose 
71 would overwrite your custom accessor. You wouldn't want that 
72 would you?
73
74 =head2 Method Modfiers
75
76 =head3 How come I can't change C<@_> in a C<before> modifier?
77
78 The C<before> modifier simply is called I<before> the main method. 
79 Its return values are simply ignored, and are B<not> passed onto 
80 the main method body. 
81
82 There are a number of reasons for this, but those arguments are 
83 too lengthy for this document. Instead, I suggest using an C<around> 
84 modifier instead. Here is some sample code:
85
86   around 'foo' => sub {
87       my $next = shift;
88       my ($self, @args) = @_;
89       # do something silly here to @args 
90       $next->($self, reverse(@args));  
91   };
92
93 =head3 How come I can't see return values in an C<after> modifier?
94
95 As with the C<before> modifier, the C<after> modifier is simply 
96 called I<after> the main method. It is passed the original contents 
97 of C<@_> and B<not> the return values of the main method. 
98
99 Again, the arguments are too lengthy as to why this has to be. And 
100 as with C<before> I recommend using an C<around> modifier instead.
101 Here is some sample code:
102
103   around 'foo' => sub {
104       my $next = shift;
105       my ($self, @args) = @_;
106       my @rv = $next->($self, @args);  
107       # do something silly with the return values
108       return reverse @rv;
109   };
110
111 =head1 AUTHOR
112
113 Stevan Little E<lt>stevan@iinteractive.comE<gt>
114
115 =head1 COPYRIGHT AND LICENSE
116
117 Copyright 2006, 2007 by Infinity Interactive, Inc.
118
119 L<http://www.iinteractive.com>
120
121 This library is free software; you can redistribute it and/or modify
122 it under the same terms as Perl itself.
123
124 =cut