bump copyright year to 2010
[gitmo/Moose.git] / lib / Moose / Manual / Construction.pod
CommitLineData
9c397ba1 1=pod
2
3=head1 NAME
4
f7435595 5Moose::Manual::Construction - Object construction (and destruction) with Moose
9c397ba1 6
7=head1 WHERE'S THE CONSTRUCTOR?
8
f293b363 9B<Do not define a C<new()> method for your classes!>
9c397ba1 10
909103e1 11When you C<use Moose> in your class, your class becomes a subclass of
12L<Moose::Object>. The L<Moose::Object> provides a C<new()> method for your
13class. If you follow our recommendations in L<Moose::Manual::BestPractices>
14and make your class immutable, then you actually get a class-specific C<new()>
088f068f 15method "inlined" in your class.
9c397ba1 16
62225ecf 17=head1 OBJECT CONSTRUCTION AND ATTRIBUTES
18
9c397ba1 19The Moose-provided constructor accepts a hash or hash reference of
20named parameters matching your attributes (actually, matching their
21C<init_arg>s). This is just another way in which Moose keeps you from
22worrying I<how> classes are implemented. Simply define a class and
23you're ready to start creating objects!
24
5384dda8 25=head1 OBJECT CONSTRUCTION HOOKS
9c397ba1 26
088f068f 27Moose lets you hook into object construction. You can validate an
241108c4 28object's state, do logging, customize construction from parameters which
29do not match your attributes, or maybe allow non-hash(ref) constructor
088f068f 30arguments. You can do this by creating C<BUILD> and/or C<BUILDARGS>
31methods.
9c397ba1 32
088f068f 33If these methods exist in your class, Moose will arrange for them to
34be called as part of the object construction process.
9c397ba1 35
36=head2 BUILDARGS
37
088f068f 38The C<BUILDARGS> method is called as a class method I<before> an
39object is created. It will receive all of the arguments that were
909103e1 40passed to C<new()> I<as-is>, and is expected to return a hash
088f068f 41reference. This hash reference will be used to construct the object,
42so it should contain keys matching your attributes' names (well,
43C<init_arg>s).
9c397ba1 44
636f25f3 45One common use for C<BUILDARGS> is to accommodate a non-hash(ref)
9c397ba1 46calling style. For example, we might want to allow our Person class to
47be called with a single argument of a social security number, C<<
48Person->new($ssn) >>.
49
5384dda8 50Without a C<BUILDARGS> method, Moose will complain, because it expects
51a hash or hash reference. We can use the C<BUILDARGS> method to
636f25f3 52accommodate this calling style:
9c397ba1 53
c30bceb8 54 around BUILDARGS => sub {
909103e1 55 my $orig = shift;
9c397ba1 56 my $class = shift;
57
909103e1 58 if ( @_ == 1 && !ref $_[0] ) {
59 return $class->$orig( ssn => $_[0] );
9c397ba1 60 }
61 else {
c30bceb8 62 return $class->$orig(@_);
9c397ba1 63 }
c30bceb8 64 };
9c397ba1 65
909103e1 66Note the call to C<< $class->$orig >>. This will call the default C<BUILDARGS>
67in L<Moose::Object>. This method takes care of distinguishing between a hash
68reference and a plain hash for you.
9c397ba1 69
70=head2 BUILD
71
72The C<BUILD> method is called I<after> an object is created. There are
909103e1 73several reasons to use a C<BUILD> method. One of the most common is to
dab94063 74check that the object state is valid. While we can validate individual
75attributes through the use of types, we can't validate the state of a
76whole object that way.
9c397ba1 77
78 sub BUILD {
79 my $self = shift;
80
81 if ( $self->country_of_residence eq 'USA' ) {
82 die 'All US residents must have an SSN'
83 unless $self->has_ssn;
84 }
85 }
86
87Another use of a C<BUILD> method could be for logging or tracking
88object creation.
89
90 sub BUILD {
91 my $self = shift;
92
5384dda8 93 debug( 'Made a new person - SSN = ', $self->ssn, );
9c397ba1 94 }
95
241108c4 96
3eb64847 97The C<BUILD> method is called with the hash reference of the parameters passed
98to the constructor (after munging by C<BUILDARGS>). This gives you a chance to
99do something with parameters that do not represent object attributes.
241108c4 100
94650eb9 101 sub BUILD {
b4538f65 102 my $self = shift;
3eb64847 103 my $args = shift;
241108c4 104
3eb64847 105 $self->add_friend(
106 My::User->new(
107 user_id => $args->{user_id},
108 )
109 );
94650eb9 110 }
241108c4 111
d67ce58f 112=head3 BUILD and parent classes
9c397ba1 113
909103e1 114The interaction between multiple C<BUILD> methods in an inheritance hierarchy
115is different from normal Perl methods. B<You should never call C<<
116$self->SUPER::BUILD >>>, nor should you ever apply a method modifier to
117C<BUILD>.
9c397ba1 118
119Moose arranges to have all of the C<BUILD> methods in a hierarchy
120called when an object is constructed, I<from parents to
121children>. This might be surprising at first, because it reverses the
122normal order of method inheritance.
123
124The theory behind this is that C<BUILD> methods can only be used for
125increasing specialization of a class's constraints, so it makes sense
f293b363 126to call the least specific C<BUILD> method first. Also, this is how
127Perl 6 does it.
9c397ba1 128
5384dda8 129=head1 OBJECT DESTRUCTION
9c397ba1 130
131Moose provides a hook for object destruction with the C<DEMOLISH>
132method. As with C<BUILD>, you should never explicitly call C<<
133$self->SUPER::DEMOLISH >>. Moose will arrange for all of the
134C<DEMOLISH> methods in your hierarchy to be called, from most to least
135specific.
136
b288593e 137Each C<DEMOLISH> method is called with a single argument.
138
5384dda8 139In most cases, Perl's built-in garbage collection is sufficient, and
088f068f 140you won't need to provide a C<DEMOLISH> method.
5384dda8 141
b288593e 142=head2 Error Handling During Destruction
143
144The interaction of object destruction and Perl's global C<$@> and C<$?>
145variables can be very confusing.
146
147Moose always localizes C<$?> when an object is being destroyed. This means
148that if you explicitly call C<exit>, that exit code will be preserved even if
149an object's destructor makes a system call.
150
151Moose also preserves C<$@> against any C<eval> calls that may happen during
152object destruction. However, if an object's C<DEMOLISH> method actually dies,
153Moose explicitly rethrows that error.
154
155If you do not like this behavior, you will have to provide your own C<DESTROY>
156method and use that instead of the one provided by L<Moose::Object>. You can
157do this to preserve C<$@> I<and> capture any errors from object destruction by
158creating an error stack.
159
9c397ba1 160=head1 AUTHOR
161
162Dave Rolsky E<lt>autarch@urth.orgE<gt>
163
164=head1 COPYRIGHT AND LICENSE
165
8e5dd3fb 166Copyright 2009-2010 by Infinity Interactive, Inc.
9c397ba1 167
168L<http://www.iinteractive.com>
169
170This library is free software; you can redistribute it and/or modify
171it under the same terms as Perl itself.
172
173=cut