more-cookin
[gitmo/Moose.git] / lib / Moose / Cookbook / Recipe3.pod
CommitLineData
471c4f09 1
2=pod
3
4=head1 NAME
5
703d9522 6Moose::Cookbook::Recipe3 - A binary tree
471c4f09 7
8=head1 SYNOPSIS
9
10 package BinaryTree;
11 use strict;
12 use warnings;
13 use Moose;
14
8597d950 15 has 'node' => (is => 'rw', isa => 'Any');
16
471c4f09 17 has 'parent' => (
8597d950 18 is => 'rw',
19 isa => 'BinaryTree',
471c4f09 20 predicate => 'has_parent',
8597d950 21 weak_ref => 1,
471c4f09 22 );
23
24 has 'left' => (
8597d950 25 is => 'rw',
26 isa => 'BinaryTree',
7c6cacb4 27 predicate => 'has_left',
28 lazy => 1,
29 default => sub { BinaryTree->new(parent => $_[0]) },
471c4f09 30 );
31
32 has 'right' => (
8597d950 33 is => 'rw',
34 isa => 'BinaryTree',
7c6cacb4 35 predicate => 'has_right',
36 lazy => 1,
37 default => sub { BinaryTree->new(parent => $_[0]) },
471c4f09 38 );
39
40 before 'right', 'left' => sub {
41 my ($self, $tree) = @_;
42 $tree->parent($self) if defined $tree;
43 };
44
45=head1 DESCRIPTION
46
8597d950 47In this recipe we take a closer look at attributes, and see how
48some of their more advanced features can be used to create fairly
49complex behaviors.
50
51The class in this recipe is a classic binary tree, each node in the
52tree is represented by an instance of the B<BinaryTree> class. Each
53instance has a C<node> slot to hold an abitrary value, a C<right>
54slot to hold the right node, a C<left> slot to hold the left node,
55and finally a C<parent> slot to hold a reference back up the tree.
56
57Now, lets start with the code, our first attribute is the C<node>
58slot, defined as such:
59
60 has 'node' => (is => 'rw', isa => 'Any');
61
62If you recall from the previous recipies, this slow will have a
63read/write accessor generated for it, and has a type constraint on it.
64The new item here is the type constraint of C<Any>. In the type
65constraint heirarchy in L<Moose::Utils::TypeConstraints>, the C<Any>
66constraint is the "root" of the hierarchy. It means exactly what it
67says, it allows anything to pass. Now, you could just as easily of left
68the C<isa> out, and left the C<node> slot unconstrainted and gotten the
69same behavior. But here, we are really including the type costraint
70for the benefit of other programmers, not the computer. It makes
71clear my intent that the C<node> can be of any type, and that the
72class is a polymorphic container. Next, lets move onto the C<parent>
73slot.
74
75 has 'parent' => (
76 is => 'rw',
77 isa => 'BinaryTree',
78 predicate => 'has_parent',
79 weak_ref => 1,
80 );
81
82As you already know from reading the previous recipes, this code
83tells you that C<parent> gets a read/write accessor, is constrainted
84to only accept instances of B<BinaryTree>. You will of course remember
85from the second recipe that the C<BinaryTree> type constraint is
86automatically created for us by Moose.
87
88The next attribute option is new though, the C<predicate> option.
89This option creates a method, which can be used to check to see if
90a given slot (in this case C<parent>) has a defined value in it. In
91this case it will create a method called C<has_parent>. Quite simple,
92and also quite handy too.
93
94This brings us to our last attribute, and also a new one. Since the
95C<parent> is a circular reference (the tree in C<parent> should
96already have a reference in either it's C<left> or C<right> nodes),
97we want to make sure that it is also a weakened reference to avoid
98memory leaks. The C<weak_ref> attribute option will do just that,
99C<weak_ref> simply takes a boolean value (C<1> or C<0>) and it will
100then add the extra capability to the accessor function to weaken
101the reference of any value stored in the C<parent> slot (1).
102
103Now, onto the C<left> and C<right> attributes. They are essentially
104the same things, only with different names, so I will just describe
105one here.
106
107 has 'left' => (
108 is => 'rw',
109 isa => 'BinaryTree',
110 predicate => 'has_left',
111 lazy => 1,
112 default => sub { BinaryTree->new(parent => $_[0]) },
113 );
114
115You already know what the C<is>, C<isa> and C<>predicate> options
116do, but now we have two more new options. These two options are
117actually linked together, in fact, you cannot use the C<lazy>
118option unless you have set the C<default> option. The class
119creation will fail with an exception (2).
120
121Before I go into detail about how C<lazy> works, let me first
122explain how C<default> works, and in particular why it is wrapped
123in a CODE ref.
124
125In the second recipe the B<BankAccount>'s C<balance> slot had a
126default value of C<0>. Since Perl will copy strings and numbers
127by value, this was all we had to say. But for any other item
128(ARRAY ref, HASH ref, object instance, etc) Perl will copy by
129reference. This means that if I were to do this:
130
131 has 'foo' => (is => 'rw', default => []);
132
133Every single instance of that class would get a pointer to the
134same ARRAY ref in their C<foo> slot. This is almost certainly
135B<not> the behavior you intended. So, the solution is to wrap
136these defaults into an anon-sub, like so:
137
138 has 'foo' => (is => 'rw', default => sub { [] });
139
140This assures that each instance of this class will get it's own
141ARRAY ref in the C<foo> slot.
142
143One other feature of the sub ref version of the C<default> option
144is that when the subroutine is executed (to get back the expected
145default value), we also pass in the instance where the slot will
146be stored. This added feature can come in quite handy at times, as
147is illustrated above, with this code:
148
149 default => sub { BinaryTree->new(parent => $_[0]) },
150
151The default value being generated is a new C<BinaryTree> instance
152for the C<left> (or C<right>) slot. Here we set up the parental
153relationship by passing the current instance to the constructor.
154
155Now, before we go on to the C<lazy> option, I want you to think
156for a moment. When an instance of this class is created, and the
157slots are being initialized, the "normal" behavior would be for
158the C<left> and C<right> slots to be populated with a new instance
159of B<BinaryTree>. In creating that instance of the C<left> or
160C<right> slots, we would need to create new instances to populate
161the C<left> and C<right> slots of I<those> instances. This would
162continue in an I<infinitely recursive spiral of death> until you had
163exhausted all available memory on your machine.
164
165This is, of course, not good :)
166
167Which brings us to the C<lazy> attribute option. The C<lazy> option
168does just what it says. It lazily initializes the slot within the
169instance. This means that it waits till the I<absolute> last possible
170moment to populate the slot. This means that if you, the user, write
171to the slot, everything happens as normal and what you pass in is stored.
172However, if you I<read> the slot, then at that I<exact> moment (and no
173sooner), the slot will be populated with the value of the C<default>
174option.
175
176This option is what allows the B<BinaryTree> class to instantiate
177objects without fear of the I<infinitely recursive spiral of death>
178I mentioned earlier.
179
180So, we have descibed a quite complex set of behaviors here, and not
181one method has needed to be written. But wait, we can't get away that
182easily. The autogenerated C<right> and C<left> accessors are not
183completely correct. They will not install the parental relationships
184that we need. We could write our own accessors, but that would require
185us to implement all those features we got automatically (the type
186constraint, the lazy initialization, etc). So instead we use the
187method modifiers again.
188
189 before 'right', 'left' => sub {
190 my ($self, $tree) = @_;
191 $tree->parent($self) if defined $tree;
192 };
193
194This is a C<before> modifier, just like we saw in the second recipe,
195but with two slight differences. First, we are applying this to more
196than one method at a time. Since both the C<left> and C<right> methods
197need the same feature, it makes sense. The second difference is that
198we are not wrapping an inherited method anymore, but instead a method
199of our own local class. Wrapping local methods is no different, the
200only requirement is that the wrappee be created before the wrapper
201(after all, you cannot wrap something which doesn't exist right?).
202
203Now, as with all the other recipes, you can go about using
204B<BinaryTree> like any other Perl 5 class. A more detailed example of
205usage can be found in F<t/003_basic.t>.
206
207=head1 CONCLUSION
208
209This recipe introduced you to some of the more advanced behavioral
210possibilities of Moose's attribute mechanism. I hope that it has
211opened your mind to the powerful possibilities of Moose. In the next
212recipe we explore how we can create custom subtypes and take
213advantage of the plethora of useful modules out on CPAN with Moose.
214
215=head1 FOOTNOTES
216
217=over 4
218
219=item (1)
220
221Weak references are tricky things, and should be used sparingly
222and appropriately (such as in the case of circular refs). If you
223are not careful, you will have slot values disappear "mysteriously"
224because perls reference counting garbage collector has gone and
225removed the item you are weak-referencing.
226
227In short, don't use them unless you know what you are doing :)
228
229=item (2)
230
231You I<can> use the C<default> option without the C<lazy> option if
232you like, as we showed in the second recipe.
233
234=back
235
471c4f09 236=head1 AUTHOR
237
238Stevan Little E<lt>stevan@iinteractive.comE<gt>
239
240=head1 COPYRIGHT AND LICENSE
241
242Copyright 2006 by Infinity Interactive, Inc.
243
244L<http://www.iinteractive.com>
245
246This library is free software; you can redistribute it and/or modify
247it under the same terms as Perl itself.
248
249=cut