Commit | Line | Data |
e67a0fca |
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 Constructors |
11 | |
12 | =head2 Accessors |
13 | |
14 | =head3 I created an attribute, where are my accessors? |
15 | |
16 | Accessors are B<not> created implicitly, you B<must> ask Moose |
17 | to create them for you. My guess is that you have this: |
18 | |
19 | has 'foo' => (isa => 'Bar'); |
20 | |
21 | when what you really want to say is: |
22 | |
23 | has 'foo' => (isa => 'Bar', is => 'rw'); |
24 | |
25 | The reason this is so, is because it is a perfectly valid use |
26 | case to I<not> have an accessor. The simplest one is that you |
27 | want to write your own. If Moose created on automatically, then |
28 | because of the order in which classes are constructed, Moose |
29 | would overwrite your custom accessor. You wouldn't want that |
30 | would you? |
31 | |
32 | =head2 Method Modfiers |
33 | |
34 | =head3 How come I can't change C<@_> in a C<before> modifier? |
35 | |
36 | The C<before> modifier simply is called I<before> the main method. |
37 | Its return values are simply ignored, and are B<not> passed onto |
38 | the main method body. |
39 | |
40 | There are a number of reasons for this, but those arguments are |
41 | too lengthy for this document. Instead, I suggest using an C<around> |
42 | modifier instead. Here is some sample code: |
43 | |
44 | around 'foo' => sub { |
45 | my $next = shift; |
46 | my ($self, @args) = @_; |
47 | # do something silly here to @args |
48 | $next->($self, reverse(@args)); |
49 | }; |
50 | |
51 | =head3 How come I can't see return values in an C<after> modifier? |
52 | |
53 | As with the C<before> modifier, the C<after> modifier is simply |
54 | called I<after> the main method. It is passed the original contents |
55 | of C<@_> and B<not> the return values of the main method. |
56 | |
57 | Again, the arguments are too lengthy as to why this has to be. And |
58 | as with C<before> I recommend using an C<around> modifier instead. |
59 | Here is some sample code: |
60 | |
61 | around 'foo' => sub { |
62 | my $next = shift; |
63 | my ($self, @args) = @_; |
64 | my @rv = $next->($self, @args); |
65 | # do something silly with the return values |
66 | return reverse @rv; |
67 | }; |
68 | |
69 | =head1 AUTHOR |
70 | |
71 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
72 | |
73 | =head1 COPYRIGHT AND LICENSE |
74 | |
75 | Copyright 2006 by Infinity Interactive, Inc. |
76 | |
77 | L<http://www.iinteractive.com> |
78 | |
79 | This library is free software; you can redistribute it and/or modify |
80 | it under the same terms as Perl itself. |
81 | |
82 | =cut |