- fixed extra comma in Perl5ObjsVsMooseObjs
[gitmo/Moose.git] / lib / Moose / Cookbook / Recipe9.pod
CommitLineData
ceb8945d 1
2=pod
3
4=head1 NAME
5
6Moose::Cookbook::Recipe9 - Builder methods and lazy_build
7
8=head1 SYNOPSIS
9
10 package BinaryTree;
11 use Moose;
12
13 has 'node' => (is => 'rw', isa => 'Any');
14
15 has 'parent' => (
16 is => 'rw',
17 isa => 'BinaryTree',
18 predicate => 'has_parent',
19 weak_ref => 1,
20 );
21
22 has 'left' => (
23 is => 'rw',
24 isa => 'BinaryTree',
25 predicate => 'has_left',
26 lazy => 1,
27 builder => '_build_child_tree',
28 );
29
30 has 'right' => (
31 is => 'rw',
32 isa => 'BinaryTree',
33 predicate => 'has_right',
34 lazy => 1,
35 builder => '_build_child_tree',
36 );
37
38 before 'right', 'left' => sub {
39 my ($self, $tree) = @_;
40 $tree->parent($self) if defined $tree;
41 };
42
43 sub _build_child_tree {
44 my $self = shift;
45
46 return BinaryTree->new( parent => $self );
47 }
48
49=head1 DESCRIPTION
50
51If you've already read L<Moose::Cookbook::Recipe3>, then this example
52should look awfully familiar. In fact, all we've done here is replace
53the attribute C<default> with a C<builder> method.
54
55In this particular case, the C<default> and C<builder> act exactly the
56same. When the C<left> or C<right> attribute is first accessed before
57it has been set, Moose will call the specified C<builder> method to
58populate the attribute.
59
60=head2 Subclassable
61
62There are some differences between C<default> and C<builder>. Because
63C<builder> is called I<by name>, it goes through Perl's normal
64inheritance system. This means that builder methods are both
65inheritable and overrideable.
66
67For example, we might make a C<BinaryTree> subclass:
68
69 package TrinaryTree;
70 use Moose;
71
72 extends 'BinaryTree';
73
74 has 'middle' => (
75 is => 'rw',
76 isa => 'BinaryTree',
77 predicate => 'has_middle',
78 lazy => 1,
79 builder => '_build_child_tree',
80 );
81
82This doesn't quite work though. If you look closely at the
83C<_build_child_tree> method defined in C<BinaryTree>, you'll notice
84that it hard-codes a class name. Naughty us!
85
86Also, as a bonus, we'll pass C<@_> through, so subclasses can override
87the method to pass additional options to the constructor.
88
89Good object-oriented code should allow itself to be subclassed
90gracefully. Let's tweak C<_build_child_tree>:
91
7f785a28 92 sub _build_child_tree {
ceb8945d 93 my $self = shift;
94
95 return (ref $self)->new( parent => $self, @_ );
96 }
97
98Now C<_build_child_tree> can be gracefully inherited and overridden.
99
100=head2 Composable
101
102There's more to builders than just subclassing, though. The fact that
103builders are called by name also makes them suitable for use in a
104role.
105
106 package HasAnimal;
107 use Moose::Role;
108
109 requires '_build_animal';
110
111 has 'animal' => (
112 is => 'ro',
113 isa => 'Animal',
114 lazy => 1,
115 builder => '_build_animal',
116 );
117
118This role provides an animal attribute, but requires that the consumer
119of the role provide a builder method it.
120
121 package CatLover;
122 use Moose;
123
124 with 'HasAnimal';
125
126 sub _build_animal {
127 return Cat->new();
128 }
129
ceb8945d 130=head2 The lazy_build shortcut
131
132The C<lazy_build> attribute parameter can be used as sugar to specify
133a whole bunch of options at once.
134
135 has 'animal' => (
136 is => 'ro',
137 isa => 'Animal',
138 lazy_build => 1,
139 );
140
141This is a shorthand for this:
142
143 has 'animal' => (
144 is => 'ro',
145 isa => 'Animal',
146 required => 1,
147 lazy => 1,
148 builder => '_build_animal',
149 predicate => 'has_animal',
150 clearer => 'clear_animal',
151 );
152
153If your attribute starts with an underscore, Moose is smart and will
154do the right thing with the C<predicate> and C<clearer>, making them
155both start with an underscore. The C<builder> method I<always> starts
156with an underscore, since you will want this to be private the vast
157majority of the time.
158
398e7b2c 159Note that the C<builder> method name is created by simply taking
160"_build_" and appending the attribute name. This means that attributes
161with a leading underscore like C<_animal> end up with a builder named
162C<_build__animal>.
163
ceb8945d 164=head1 CONCLUSION
165
166The C<builder> option is a more OO-friendly version of the C<default>
167functionality. It also has the property of separating out the code
168into a separate well-defined method. This alone makes it valuable. It
169is quite ugly to jam a long default code reference into your attribute
170definition.
171
172Here are some good rules for determining when to use C<builder> vs
173C<default>.
174
175If the default value is a simple scalar that only needs to be
176calculated once (or a constant), use C<default>.
177
178If the default value is an empty reference that needs to be wrapped in
179a coderef like C<sub { [] }>, use C<default>.
180
181Otherwise, use C<builder>.
182
09b815aa 183This ensures that your classes are easily subclassable, and also helps
184keep crufty code out of your attribute definition blocks.
185
ceb8945d 186=head1 AUTHOR
187
188Dave Rolsky E<lt>autarch@urth.orgE<gt>
189
190=head1 COPYRIGHT AND LICENSE
191
192Copyright 2006-2008 by Infinity Interactive, Inc.
193
194L<http://www.iinteractive.com>
195
196This library is free software; you can redistribute it and/or modify
197it under the same terms as Perl itself.
198
199=cut