More polishing of this doc
[gitmo/Moose.git] / lib / Moose / Manual / Construction.pod
CommitLineData
9c397ba1 1=pod
2
3=head1 NAME
4
5Moose::Manual::Classes - Object construction (and destruction) with Moose
6
7=head1 WHERE'S THE CONSTRUCTOR?
8
5384dda8 9B<You do not need to define a C<new()> method for your classes!>
9c397ba1 10
11When you C<use Moose> in your class, you will become a subclass of
12C<Moose::Object>, which provides a C<new> method for you. And if you
5384dda8 13follow L<our recommendations|Moose::Manual::BestPractices> and make
14your class immutable, then you actually get a class-specific C<new>
15method genreated in your class.
9c397ba1 16
17The Moose-provided constructor accepts a hash or hash reference of
18named parameters matching your attributes (actually, matching their
19C<init_arg>s). This is just another way in which Moose keeps you from
20worrying I<how> classes are implemented. Simply define a class and
21you're ready to start creating objects!
22
5384dda8 23=head1 OBJECT CONSTRUCTION HOOKS
9c397ba1 24
25Sometimes you need to hook into object construction. Some common needs
26are validating an object's state, logging, and allowing non-hash(ref)
27constructor arguments. Moose provides hooks for these needs with the
28C<BUILD> and C<BUILDARGS> methods.
29
30If these are defined in your class, then Moose will arrange for them
31to be called as part of the object construction process.
32
33=head2 BUILDARGS
34
35The C<BUILDARGS> method is called I<before> an object is created, and
36is therefore called as a class method. It will receive all of the
37arguments that were passed to C<new> I<as-is>. Your C<BUILDARGS>
38method must then return a hash reference. This hash reference will be
39used to construct the object, so it should contain keys matching your
40attributes' names (well, C<init_arg>s).
41
42One common use for C<BUILDARGS> is to accomodate a non-hash(ref)
43calling style. For example, we might want to allow our Person class to
44be called with a single argument of a social security number, C<<
45Person->new($ssn) >>.
46
5384dda8 47Without a C<BUILDARGS> method, Moose will complain, because it expects
48a hash or hash reference. We can use the C<BUILDARGS> method to
49accomodate this calling style:
9c397ba1 50
51 sub BUILDARGS {
52 my $class = shift;
53
54 if ( @_ == 1 && ! ref $_[0] ) {
55 return { ssn => $_[0] };
56 }
57 else {
58 return $class->SUPER::BUILDARGS(@_);
59 }
60 }
61
62Note the call to C<SUPER::BUILDARGS>. This will call the default
63C<BUILDARGS> in C<Moose::Object>. This method handles distinguishing
64between a hash reference and a plain hash, so you don't have to.
65
66=head2 BUILD
67
68The C<BUILD> method is called I<after> an object is created. There are
69many potential uses for a C<BUILD> method. One of the most common is
70to check that the object state makes sense. While we can validate
71individual attributes through the use of types, we can't validate the
72state of a whole object that way.
73
74 sub BUILD {
75 my $self = shift;
76
77 if ( $self->country_of_residence eq 'USA' ) {
78 die 'All US residents must have an SSN'
79 unless $self->has_ssn;
80 }
81 }
82
83Another use of a C<BUILD> method could be for logging or tracking
84object creation.
85
86 sub BUILD {
87 my $self = shift;
88
5384dda8 89 debug( 'Made a new person - SSN = ', $self->ssn, );
9c397ba1 90 }
91
92=head3 BUILD and Parent Classes
93
94The interaction between multiple C<BUILD> methods in an inheritance
95hierarchy is different from normal Perl methods. B<You should never
96call C<< $self->SUPER::BUILD >>.>
97
98Moose arranges to have all of the C<BUILD> methods in a hierarchy
99called when an object is constructed, I<from parents to
100children>. This might be surprising at first, because it reverses the
101normal order of method inheritance.
102
103The theory behind this is that C<BUILD> methods can only be used for
104increasing specialization of a class's constraints, so it makes sense
105to call the least specific first (also, this is how Perl 6 does it).
106
5384dda8 107=head1 OBJECT DESTRUCTION
9c397ba1 108
109Moose provides a hook for object destruction with the C<DEMOLISH>
110method. As with C<BUILD>, you should never explicitly call C<<
111$self->SUPER::DEMOLISH >>. Moose will arrange for all of the
112C<DEMOLISH> methods in your hierarchy to be called, from most to least
113specific.
114
5384dda8 115In most cases, Perl's built-in garbage collection is sufficient, and
116you won't need ot provide a C<DEMOLISH> method.
117
9c397ba1 118=head1 AUTHOR
119
120Dave Rolsky E<lt>autarch@urth.orgE<gt>
121
122=head1 COPYRIGHT AND LICENSE
123
124Copyright 2008 by Infinity Interactive, Inc.
125
126L<http://www.iinteractive.com>
127
128This library is free software; you can redistribute it and/or modify
129it under the same terms as Perl itself.
130
131=cut