1Moose::Cookbook::BasicsU:s:ePreMrCosooonsnte_r:Bi:UbCIuoLtoDekAdbRoGPoSekAr:nl:dBBDaUosIciLucDms(e:3n:)tPaetrisoonn_BUILDARGSAndBUILD(3)
2
3
4
6 Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD - Using BUILDARGS and
7 BUILD to hook into object construction
8
10 version 2.2014
11
13 package Person;
14
15 has 'ssn' => (
16 is => 'ro',
17 isa => 'Str',
18 predicate => 'has_ssn',
19 );
20
21 has 'country_of_residence' => (
22 is => 'ro',
23 isa => 'Str',
24 default => 'usa'
25 );
26
27 has 'first_name' => (
28 is => 'ro',
29 isa => 'Str',
30 );
31
32 has 'last_name' => (
33 is => 'ro',
34 isa => 'Str',
35 );
36
37 around BUILDARGS => sub {
38 my $orig = shift;
39 my $class = shift;
40
41 if ( @_ == 1 && ! ref $_[0] ) {
42 return $class->$orig(ssn => $_[0]);
43 }
44 else {
45 return $class->$orig(@_);
46 }
47 };
48
49 sub BUILD {
50 my $self = shift;
51
52 if ( $self->country_of_residence eq 'usa' ) {
53 die 'Cannot create a Person who lives in the USA without an ssn.'
54 unless $self->has_ssn;
55 }
56 }
57
59 This recipe demonstrates the use of "BUILDARGS" and "BUILD". By
60 defining these methods, we can hook into the object construction
61 process without overriding "new".
62
63 The "BUILDARGS" method is called before an object has been created. It
64 is called as a class method, and receives all of the parameters passed
65 to the "new" method. It is expected to do something with these
66 arguments and return a hash reference. The keys of the hash must be
67 attribute "init_arg"s.
68
69 The primary purpose of "BUILDARGS" is to allow a class to accept
70 something other than named arguments. In the case of our "Person"
71 class, we are allowing it to be called with a single argument, a social
72 security number:
73
74 my $person = Person->new('123-45-6789');
75
76 The key part of our "BUILDARGS" is this conditional:
77
78 if ( @_ == 1 && ! ref $_[0] ) {
79 return $class->$orig(ssn => $_[0]);
80 }
81
82 By default, Moose constructors accept a list of key-value pairs, or a
83 hash reference. We need to make sure that $_[0] is not a reference
84 before assuming it is a social security number.
85
86 We call the original "BUILDARGS" method to handle all the other cases.
87 You should always do this in your own "BUILDARGS" methods, since
88 Moose::Object provides its own "BUILDARGS" method that handles hash
89 references and a list of key-value pairs.
90
91 The "BUILD" method is called after the object is constructed, but
92 before it is returned to the caller. The "BUILD" method provides an
93 opportunity to check the object state as a whole. This is a good place
94 to put logic that cannot be expressed as a type constraint on a single
95 attribute.
96
97 In the "Person" class, we need to check the relationship between two
98 attributes, "ssn" and "country_of_residence". We throw an exception if
99 the object is not logically consistent.
100
102 This recipe is made significantly simpler because all of the attributes
103 are read-only. If the "country_of_residence" attribute were settable,
104 we would need to check that a Person had an "ssn" if the new country
105 was "usa". This could be done with a "before" modifier.
106
108 We have repeatedly discouraged overriding "new" in Moose classes. This
109 recipe shows how you can use "BUILDARGS" and "BUILD" to hook into
110 object construction without overriding "new".
111
112 The "BUILDARGS" method lets us expand on Moose's built-in parameter
113 handling for constructors. The "BUILD" method lets us implement logical
114 constraints across the whole object after it is created.
115
117 • Stevan Little <stevan@cpan.org>
118
119 • Dave Rolsky <autarch@urth.org>
120
121 • Jesse Luehrs <doy@cpan.org>
122
123 • Shawn M Moore <sartak@cpan.org>
124
125 • יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
126
127 • Karen Etheridge <ether@cpan.org>
128
129 • Florian Ragwitz <rafl@debian.org>
130
131 • Hans Dieter Pearcey <hdp@cpan.org>
132
133 • Chris Prather <chris@prather.org>
134
135 • Matt S Trout <mstrout@cpan.org>
136
138 This software is copyright (c) 2006 by Infinity Interactive, Inc.
139
140 This is free software; you can redistribute it and/or modify it under
141 the same terms as the Perl 5 programming language system itself.
142
143
144
145perl v5.32.1 Moose::C2o0o2k1b-o0o1k-:2:7Basics::Person_BUILDARGSAndBUILD(3)