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