Change method_metaclass to an attr for metarole application.
[gitmo/Moose.git] / lib / Moose / Cookbook / Basics / Recipe11.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::Basics::Recipe11 - Using BUILDARGS and BUILD to hook into object construction
7
8 =head1 SYNOPSIS
9
10   package Person;
11
12   has 'ssn' => (
13       is        => 'ro',
14       isa       => 'Str',
15       predicate => 'has_ssn',
16   );
17
18   has 'country_of_residence' => (
19       is      => 'ro',
20       isa     => 'Str',
21       default => 'usa'
22   );
23
24   has 'first_name' => (
25       is  => 'ro',
26       isa => 'Str',
27   );
28
29   has 'last_name' => (
30       is  => 'ro',
31       isa => 'Str',
32   );
33
34   sub BUILDARGS {
35       my $class = shift;
36
37       if ( @_ == 1 && ! ref $_[0] ) {
38           return { ssn => $_[0] };
39       }
40       else {
41           return $class->SUPER::BUILDARGS(@_);
42       }
43   }
44
45   sub BUILD {
46       my $self = shift;
47
48       if ( $self->country_of_residence eq 'usa' ) {
49           die 'Cannot create a Person who lives in the USA without an ssn.'
50               unless $self->has_ssn;
51       }
52   }
53
54 =head1 DESCRIPTION
55
56 This recipe demonstrates the use of C<BUILDARGS> and C<BUILD>. By
57 defining these methods, we can hook into the object construction
58 process without overriding C<new>.
59
60 The C<BUILDARGS> method is called I<before> an object has been
61 created. It is called as a class method, and receives all of the
62 parameters passed to the C<new> method. It is expected to do something
63 with these arguments and return a hash reference. The keys of the hash
64 must be attribute C<init_arg>s.
65
66 The primary purpose of C<BUILDARGS> is to allow a class to accept
67 something other than named arguments. In the case of our C<Person>
68 class, we are allowing it to be called with a single argument, a
69 social security number:
70
71   my $person = Person->new('123-45-6789');
72
73 The key part of our C<BUILDARGS> is this conditional:
74
75       if ( @_ == 1 && ! ref $_[0] ) {
76           return { ssn => $_[0] };
77       }
78
79 By default, Moose constructors accept a list of key-value pairs, or a
80 hash reference. We need to make sure that C<$_[0]> is not a reference
81 before assuming it is a social security number.
82
83 We call C<< $class->SUPER::BUILDARGS(@_) >> to handle all the other
84 cases. You should always do this in your own C<BUILDARGS> methods,
85 since L<Moose::Object> provides its own C<BUILDARGS> method that
86 handles hash references and a list of key-value pairs.
87
88 The C<BUILD> method is called I<after> the object is constructed, but
89 before it is returned to the caller. The C<BUILD> method provides an
90 opportunity to check the object state as a whole. This is a good place
91 to put logic that cannot be expressed as a type constraint on a single
92 attribute.
93
94 In the C<Person> class, we need to check the relationship between two
95 attributes, C<ssn> and C<country_of_residence>. We throw an exception
96 if the object is not logically consistent.
97
98 =head1 MORE CONSIDERATIONS
99
100 This recipe is made significantly simpler because all of the
101 attributes are read-only. If the C<country_of_residence> attribute
102 were settable, we would need to check that a Person had an C<ssn> if
103 the new country was C<usa>. This could be done with a C<before>
104 modifier.
105
106 =head1 CONCLUSION
107
108 We have repeatedly discouraged overriding C<new> in Moose
109 classes. This recipe shows how you can use C<BUILDARGS> and C<BUILD>
110 to hook into object construction without overriding C<new>
111
112 The C<BUILDARGS> method lets us expand on Moose's built-in parameter
113 handling for constructors. The C<BUILD> method lets us implement
114 logical constraints across the whole object after it is created.
115
116 =head1 AUTHOR
117
118 Dave Rolsky E<lt>autarch@urth.orgE<gt>
119
120 =head1 COPYRIGHT AND LICENSE
121
122 Copyright 2006-2009 by Infinity Interactive, Inc.
123
124 L<http://www.iinteractive.com>
125
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself.
128
129 =cut