Commit | Line | Data |
8d38e631 |
1 | =pod |
2 | |
3 | =head1 NAME |
4 | |
6c384d50 |
5 | Moose::Manual::Attributes - Object attributes with Moose |
8d38e631 |
6 | |
7 | =head1 INTRODUCTION |
8 | |
0c38f882 |
9 | Moose attributes have many properties, and attributes are probably the |
10 | single most powerful and flexible part of Moose. You can create a |
11 | powerful class simply by declaring attributes. In fact, it's possible |
12 | to have classes that consist solely of attribute declarations. |
8d38e631 |
13 | |
0c38f882 |
14 | An attribute is a property that every member of a class has. For |
c5934de1 |
15 | example, we might say that "every C<Person> object has a first name and |
16 | last name". Attributes can be optional, so that we can say "some C<Person> |
8d38e631 |
17 | objects have a social security number (and some don't)". |
18 | |
d4b96efa |
19 | At its simplest, an attribute can be thought of as a named value (as |
20 | in a hash) that can be read and set. However, attributes can also have |
21 | defaults, type constraints, delegation and much more. |
8d38e631 |
22 | |
0c38f882 |
23 | In other languages, attributes are also referred to as slots or |
24 | properties. |
25 | |
0b9488c8 |
26 | =head1 ATTRIBUTE OPTIONS |
8d38e631 |
27 | |
28 | Use the C<has> function to declare an attribute: |
29 | |
30 | package Person; |
31 | |
32 | use Moose; |
33 | |
34 | has 'first_name' => ( is => 'rw' ); |
35 | |
c5934de1 |
36 | This says that all C<Person> objects have an optional read-write |
d4b96efa |
37 | "first_name" attribute. |
8d38e631 |
38 | |
d67ce58f |
39 | =head2 Read-write Vs read-only |
8d38e631 |
40 | |
0c38f882 |
41 | The options passed to C<has> define the properties of the |
42 | attribute. There are a many options, but in the simplest form you just |
43 | need to set C<is>, which can be either C<rw> (read-write) or C<ro> |
44 | (read-only). |
8d38e631 |
45 | |
0c38f882 |
46 | (In fact, you could even omit C<is>, but that gives you an attribute |
47 | that has no accessors, which is pointless unless you're doing some |
48 | deep, dark magic). |
8d38e631 |
49 | |
d67ce58f |
50 | =head2 Accessor methods |
8d38e631 |
51 | |
52 | Each attribute has one or more accessor methods. An accessor lets you |
d4b96efa |
53 | read and write the value of that attribute for an object. |
8d38e631 |
54 | |
55 | By default, the accessor method has the same name as the attribute. If |
56 | you declared your attribute as C<ro> then your accessor will be |
57 | read-only. If you declared it read-write, you get a read-write |
58 | accessor. Simple. |
59 | |
c5934de1 |
60 | Given our C<Person> example above, we now have a single C<first_name> |
61 | accessor that can read or write a C<Person> object's C<first_name> |
62 | attribute's value. |
8d38e631 |
63 | |
64 | If you want, you can also explicitly specify the method names to be |
0b9488c8 |
65 | used for reading and writing an attribute's value. This is |
636f25f3 |
66 | particularly handy when you'd like an attribute to be publicly |
8d38e631 |
67 | readable, but only privately settable. For example: |
68 | |
d983b81e |
69 | has 'weight' => ( |
70 | is => 'rw', |
71 | writer => '_set_weight', |
72 | ); |
8d38e631 |
73 | |
74 | This might be useful if weight is calculated based on other methods, |
75 | for example every time the C<eat> method is called, we might adjust |
76 | weight. This lets us hide the implementation details of weight |
77 | changes, but still provide the weight value to users of the class. |
78 | |
0b9488c8 |
79 | Some people might prefer to have distinct methods for reading and |
d4b96efa |
80 | writing. In I<Perl Best Practices>, Damian Conway recommends that |
81 | reader methods start with "get_" and writer methods start with "set_". |
8d38e631 |
82 | |
83 | We can do exactly that by providing names for both the C<reader> and |
84 | C<writer> methods: |
85 | |
d983b81e |
86 | has 'weight' => ( |
87 | is => 'rw', |
88 | reader => 'get_weight', |
89 | writer => 'set_weight', |
90 | ); |
8d38e631 |
91 | |
92 | If you're thinking that doing this over and over would be insanely |
93 | tedious, you're right! Fortunately, Moose provides a powerful |
0c38f882 |
94 | extension system that lets override the default naming |
95 | conventions. See L<Moose::Manual::MooseX> for more details. |
8d38e631 |
96 | |
d67ce58f |
97 | =head2 Predicate and clearer methods |
8d38e631 |
98 | |
d4b96efa |
99 | Moose allows you to explicitly distinguish between a false or |
0c38f882 |
100 | undefined attribute value and an attribute which has not been set. If |
101 | you want to access this information, you must define clearer and |
d4b96efa |
102 | predicate methods for an attribute. |
8d38e631 |
103 | |
d4b96efa |
104 | A predicate method tells you whether or not a given attribute is |
c5934de1 |
105 | currently set. Note an attribute can be explicitly set to C<undef> or |
0c38f882 |
106 | some other false value, but the predicate will return true. |
8d38e631 |
107 | |
108 | The clearer method unsets the attribute. This is I<not> the |
109 | same as setting the value to C<undef>, but you can only distinguish |
110 | between them if you define a predicate method! |
111 | |
112 | Here's some code to illustrate the relationship between an accessor, |
113 | predicate, and clearer method. |
114 | |
115 | package Person; |
116 | |
117 | use Moose; |
118 | |
d983b81e |
119 | has 'ssn' => ( |
120 | is => 'rw', |
121 | clearer => 'clear_ssn', |
122 | predicate => 'has_ssn', |
123 | ); |
8d38e631 |
124 | |
125 | ... |
126 | |
127 | my $person = Person->new(); |
128 | $person->has_ssn; # false |
129 | |
130 | $person->ssn(undef); |
131 | $person->ssn; # returns undef |
132 | $person->has_ssn; # true |
133 | |
134 | $person->clear_ssn; |
135 | $person->ssn; # returns undef |
136 | $person->has_ssn; # false |
137 | |
138 | $person->ssn('123-45-6789'); |
139 | $person->ssn; # returns '123-45-6789' |
140 | $person->has_ssn; # true |
141 | |
142 | my $person2 = Person->new( ssn => '111-22-3333'); |
143 | $person2->has_ssn; # true |
144 | |
d4b96efa |
145 | By default, Moose does not make a predicate or clearer for you. You |
0c38f882 |
146 | must explicitly provide names for them. |
8d38e631 |
147 | |
d67ce58f |
148 | =head2 Required or not? |
8d38e631 |
149 | |
d4b96efa |
150 | By default, all attributes are optional, and do not need to be |
151 | provided at object construction time. If you want to make an attribute |
152 | required, simply set the C<required> option to true: |
8d38e631 |
153 | |
d983b81e |
154 | has 'name' => ( |
155 | is => 'rw', |
156 | required => 1, |
157 | ); |
8d38e631 |
158 | |
159 | There are a couple caveats worth mentioning in regards to what |
c5934de1 |
160 | "required" actually means. |
8d38e631 |
161 | |
c5934de1 |
162 | Basically, all it says is that this attribute (C<name>) must be provided |
d4b96efa |
163 | to the constructor. It does not say anything about its value, so it |
164 | could be C<undef>. |
8d38e631 |
165 | |
166 | If you define a clearer method on a required attribute, the clearer |
d4b96efa |
167 | I<will> work, so even a required attribute can be unset after object |
168 | construction. |
8d38e631 |
169 | |
d4b96efa |
170 | This means that if you do make an attribute required, providing a |
171 | clearer doesn't make much sense. In some cases, it might be handy to |
172 | have a I<private> C<clearer> and C<predicate> for a required |
173 | attribute. |
8d38e631 |
174 | |
d67ce58f |
175 | =head2 Default and builder methods |
8d38e631 |
176 | |
d4b96efa |
177 | Attributes can have default values, and Moose provides two ways to |
178 | specify that default. |
8d38e631 |
179 | |
180 | In the simplest form, you simply provide a non-reference scalar value |
0b9488c8 |
181 | for the C<default> option: |
8d38e631 |
182 | |
d983b81e |
183 | has 'size' => ( |
184 | is => 'rw', |
185 | default => 'medium', |
186 | predicate => 'has_size', |
187 | ); |
8d38e631 |
188 | |
189 | If the size attribute is not provided to the constructor, then it ends |
c5934de1 |
190 | up being set to C<medium>: |
8d38e631 |
191 | |
192 | my $person = Person->new(); |
193 | $person->size; # medium |
194 | $person->has_size; # true |
195 | |
0b9488c8 |
196 | You can also provide a subroutine reference for C<default>. This |
0c38f882 |
197 | reference will be called as a method on the object. |
8d38e631 |
198 | |
d983b81e |
199 | has 'size' => ( |
200 | is => 'rw', |
201 | default => |
202 | sub { ( 'small', 'medium', 'large' )[ int( rand 3 ) ] }, |
203 | predicate => 'has_size', |
204 | ); |
8d38e631 |
205 | |
206 | This is dumb example, but it illustrates the point that the subroutine |
207 | will be called for every new object created. |
208 | |
d4b96efa |
209 | Of course, if it's called during object construction, it may be called |
210 | before other attributes have been set. If your default is dependent on |
0c38f882 |
211 | other parts of the object's state, you can make the attribute |
212 | C<lazy>. Laziness is covered in the next section. |
8d38e631 |
213 | |
214 | If you want to use a reference of any sort as the default value, you |
215 | must return it from a subroutine. This is necessary because otherwise |
216 | Perl would instantiate the reference exactly once, and it would be |
217 | shared by all objects: |
218 | |
d983b81e |
219 | has 'mapping' => ( |
220 | is => 'rw', |
221 | default => {}, # wrong! |
222 | ); |
8d38e631 |
223 | |
d4b96efa |
224 | Moose will throw an error if you pass a bare non-subroutine reference |
225 | as the default. |
226 | |
8d38e631 |
227 | If Moose allowed this then the default mapping attribute could easily |
228 | end up shared across many objects. Instead, wrap it in a subroutine |
229 | reference: |
230 | |
d983b81e |
231 | has 'mapping' => ( |
232 | is => 'rw', |
233 | default => sub { {} }, # right! |
234 | ); |
8d38e631 |
235 | |
236 | This is a bit awkward, but it's just the way Perl works. |
237 | |
238 | As an alternative to using a subroutine reference, you can instead |
0b9488c8 |
239 | supply a C<builder> method for your attribute: |
8d38e631 |
240 | |
d983b81e |
241 | has 'size' => ( |
242 | is => 'rw', |
243 | builder => '_build_size', |
244 | predicate => 'has_size', |
245 | ); |
8d38e631 |
246 | |
247 | sub _build_size { |
d983b81e |
248 | return ( 'small', 'medium', 'large' )[ int( rand 3 ) ]; |
8d38e631 |
249 | } |
250 | |
251 | This has several advantages. First, it moves a chunk of code to its |
252 | own named method, which improves readability and code |
ba5d9201 |
253 | organization. |
8d38e631 |
254 | |
0b9488c8 |
255 | We strongly recommend that you use a C<builder> instead of a |
256 | C<default> for anything beyond the most trivial default. |
8d38e631 |
257 | |
ba5d9201 |
258 | =head3 Builders allow subclassing |
259 | |
260 | Because the C<builder> is called I<by name>, it goes through Perl's |
261 | method resolution. This means that builder methods are both |
262 | inheritable and overridable. |
263 | |
264 | If we subclass our C<Person> class, we can override C<_build_size>: |
265 | |
266 | package Lilliputian; |
267 | |
268 | use Moose; |
269 | extends 'Person'; |
270 | |
271 | sub _build_size { return 'small' } |
272 | |
273 | =head3 Builders can be composed from roles |
274 | |
275 | Because builders are called by name, they work well with roles. For |
276 | example, a role could provide an attribute but require that the |
277 | consuming class provide the C<builder>: |
278 | |
279 | package HasSize; |
280 | use Moose::Role; |
281 | |
282 | requires '_build_size'; |
283 | |
284 | has 'size' => ( |
285 | is => 'ro', |
286 | lazy => 1, |
287 | builder => '_build_animal', |
288 | ); |
289 | |
290 | package Lilliputian; |
291 | use Moose; |
292 | |
293 | with 'HasSize'; |
294 | |
295 | sub _build_size { return 'small' } |
296 | |
297 | Roles are covered in L<Moose::Manual::Roles>. |
298 | |
d67ce58f |
299 | =head2 Laziness and C<lazy_build> |
8d38e631 |
300 | |
0b9488c8 |
301 | Moose lets you defer attribute population by making an attribute |
302 | C<lazy>: |
8d38e631 |
303 | |
d983b81e |
304 | has 'size' => ( |
305 | is => 'rw', |
306 | lazy => 1, |
307 | builder => '_build_size', |
308 | ); |
8d38e631 |
309 | |
0c38f882 |
310 | When C<lazy> is true, the default is not generated until the reader |
d4b96efa |
311 | method is called, rather than at object construction time. There are |
312 | several reasons you might choose to do this. |
8d38e631 |
313 | |
314 | First, if the default value for this attribute depends on some other |
0b9488c8 |
315 | attributes, then the attribute I<must> be C<lazy>. During object |
d4b96efa |
316 | construction, defaults are not generated in a predictable order, so |
0c38f882 |
317 | you cannot count on some other attribute being populated when |
318 | generating a default. |
8d38e631 |
319 | |
d4b96efa |
320 | Second, there's often no reason to calculate a default before it's |
321 | needed. Making an attribute C<lazy> lets you defer the cost until the |
322 | attribute is needed. If the attribute is I<never> needed, you save |
323 | some CPU time. |
8d38e631 |
324 | |
325 | We recommend that you make any attribute with a builder or non-trivial |
0b9488c8 |
326 | default C<lazy> as a matter of course. |
8d38e631 |
327 | |
328 | To facilitate this, you can simply specify the C<lazy_build> attribute |
329 | option. This bundles up a number of options together: |
330 | |
d983b81e |
331 | has 'size' => ( |
332 | is => 'rw', |
333 | lazy_build => 1, |
334 | ); |
8d38e631 |
335 | |
336 | This is the same as specifying all of these options: |
337 | |
d983b81e |
338 | has 'size' => ( |
339 | is => 'rw', |
340 | lazy => 1, |
341 | builder => '_build_size', |
342 | clearer => 'clear_size', |
343 | predicate => 'has_size', |
344 | ); |
8d38e631 |
345 | |
0c39debe |
346 | If your attribute name starts with an underscore (C<_>), then the clearer |
8d38e631 |
347 | and predicate will as well: |
348 | |
d983b81e |
349 | has '_size' => ( |
350 | is => 'rw', |
351 | lazy_build => 1, |
352 | ); |
8d38e631 |
353 | |
0b9488c8 |
354 | becomes: |
8d38e631 |
355 | |
d983b81e |
356 | has '_size' => ( |
357 | is => 'rw', |
358 | lazy => 1, |
359 | builder => '_build__size', |
360 | clearer => '_clear_size', |
361 | predicate => '_has_size', |
362 | ); |
8d38e631 |
363 | |
0b9488c8 |
364 | Note the doubled underscore in the builder name. Internally, Moose |
365 | simply prepends the attribute name with "_build_" to come up with the |
366 | builder name. |
8d38e631 |
367 | |
0b9488c8 |
368 | If you don't like the names that C<lazy_build> generates, you can |
369 | always provide your own: |
370 | |
d983b81e |
371 | has 'size' => ( |
372 | is => 'rw', |
373 | lazy_build => 1, |
374 | clearer => '_clear_size', |
375 | ); |
0b9488c8 |
376 | |
377 | Options that you explicitly provide are always used in favor of |
378 | Moose's internal defaults. |
379 | |
d67ce58f |
380 | =head2 Constructor parameters (C<init_arg>) |
d4b96efa |
381 | |
382 | By default, each attribute can be passed by name to the class's |
636f25f3 |
383 | constructor. On occasion, you may want to use a different name for |
d4b96efa |
384 | the constructor parameter. You may also want to make an attribute |
0c38f882 |
385 | unsettable via the constructor. |
d4b96efa |
386 | |
0c38f882 |
387 | Both of these goals can be accomplished with the C<init_arg> option: |
d4b96efa |
388 | |
d983b81e |
389 | has 'bigness' => ( |
390 | is => 'rw', |
391 | init_arg => 'size', |
392 | ); |
d4b96efa |
393 | |
0c39debe |
394 | Now we have an attribute named "bigness", but we pass C<size> to the |
0c38f882 |
395 | constructor. |
d4b96efa |
396 | |
0c38f882 |
397 | Even more useful is the ability to disable setting an attribute via |
398 | the constructor. This is particularly handy for private attributes: |
d4b96efa |
399 | |
d983b81e |
400 | has '_genetic_code' => ( |
0c38f882 |
401 | is => 'rw', |
402 | lazy_build => 1, |
403 | init_arg => undef, |
d983b81e |
404 | ); |
d4b96efa |
405 | |
406 | By setting the C<init_arg> to C<undef>, we make it impossible to set |
407 | this attribute when creating a new object. |
408 | |
d67ce58f |
409 | =head2 Weak references |
0b9488c8 |
410 | |
411 | Moose has built-in support for weak references. If you set the |
d4b96efa |
412 | C<weak_ref> option to a true value, then it will call |
0b9488c8 |
413 | C<Scalar::Util::weaken> whenever the attribute is set: |
414 | |
d983b81e |
415 | has 'parent' => ( |
416 | is => 'rw', |
417 | weak_ref => 1, |
418 | ); |
0b9488c8 |
419 | |
420 | $node->parent($parent_node); |
421 | |
422 | This is very useful when you're building objects that may contain |
423 | circular references. |
424 | |
425 | =head2 Triggers |
426 | |
d4b96efa |
427 | A C<trigger> is a subroutine that is called whenever the attribute is |
428 | set: |
0b9488c8 |
429 | |
d983b81e |
430 | has 'size' => ( |
431 | is => 'rw', |
432 | trigger => \&_size_set, |
433 | ); |
0b9488c8 |
434 | |
435 | sub _size_set { |
436 | my ( $self, $size, $meta_attr ) = @_; |
437 | |
d4b96efa |
438 | warn $self->name, " size is now $size\n"; |
0b9488c8 |
439 | } |
440 | |
441 | The trigger is called as a method, and receives the new value as well |
d4b96efa |
442 | as the L<Moose::Meta::Attribute> object for the attribute. The trigger |
443 | is called I<after> the value is set. |
0b9488c8 |
444 | |
9c9484bf |
445 | This differs from an after method modifier in two ways. First, a |
446 | trigger is only called when the attribute is set, as opposed to |
447 | whenever the accessor is called. Second, it is also called if the |
448 | attribute is set via a lazy default or builder. |
449 | |
d67ce58f |
450 | =head2 Attribute types |
0b9488c8 |
451 | |
d4b96efa |
452 | Attributes can be restricted to only accept certain types: |
0b9488c8 |
453 | |
d983b81e |
454 | has 'first_name' => ( |
455 | is => 'rw', |
456 | isa => 'Str', |
457 | ); |
0b9488c8 |
458 | |
0c39debe |
459 | This says that the C<first_name> attribute must be a string. |
0b9488c8 |
460 | |
461 | Moose also provides a shortcut for specifying that an attribute only |
462 | accepts objects that do a certain role: |
463 | |
d983b81e |
464 | has 'weapon' => ( |
465 | is => 'rw', |
466 | does => 'MyApp::Weapon', |
467 | ); |
0b9488c8 |
468 | |
469 | See the L<Moose::Manual::Types> documentation for a complete |
470 | discussion of Moose's type system. |
471 | |
472 | =head2 Delegation |
473 | |
0c39debe |
474 | Attributes can define methods which simply delegate to their values: |
0b9488c8 |
475 | |
d983b81e |
476 | has 'hair_color' => ( |
477 | is => 'rw', |
478 | isa => 'Graphics::Color::RGB', |
479 | handles => { hair_color_hex => 'as_hex_string' }, |
480 | ); |
0b9488c8 |
481 | |
d4b96efa |
482 | This adds a new method, C<hair_color_hex>. When someone calls |
483 | C<hair_color_hex>, internally, the object just calls C<< |
484 | $self->hair_color->as_hex_string >>. |
0b9488c8 |
485 | |
0c38f882 |
486 | See L<Moose::Manual::Delegation> for documentation on how to set up |
0b9488c8 |
487 | delegation methods. |
488 | |
489 | =head2 Metaclass and traits |
490 | |
491 | One of Moose's best features is that it can be extended in all sorts |
d4b96efa |
492 | of ways through the use of custom metaclasses and metaclass traits. |
0b9488c8 |
493 | |
494 | When declaring an attribute, you can declare a metaclass or a set of |
495 | traits for the attribute: |
496 | |
497 | use MooseX::AttributeHelpers; |
498 | |
d983b81e |
499 | has 'mapping' => ( |
500 | metaclass => 'Collection::Hash', |
501 | is => 'ro', |
502 | default => sub { {} }, |
503 | ); |
0b9488c8 |
504 | |
505 | In this case, the metaclass C<Collection::Hash> really refers to |
0c39debe |
506 | L<MooseX::AttributeHelpers::Collection::Hash>. |
0b9488c8 |
507 | |
508 | You can also apply one or more traits to an attribute: |
509 | |
0b9488c8 |
510 | use MooseX::MetaDescription; |
511 | |
d983b81e |
512 | has 'size' => ( |
513 | is => 'rw', |
514 | traits => ['MooseX::MetaDescription::Meta::Trait'], |
515 | description => { |
516 | html_widget => 'text_input', |
517 | serialize_as => 'element', |
518 | }, |
519 | ); |
0b9488c8 |
520 | |
521 | The advantage of traits is that you can mix more than one of them |
522 | together easily (in fact, a trait is just a role under the hood). |
523 | |
524 | There are a number of MooseX modules on CPAN which provide useful |
525 | attribute metaclasses and traits. See L<Moose::Manual::MooseX> for |
526 | some examples. You can also write your own metaclasses and traits. See |
527 | the "Meta" and "Extending" recipes in L<Moose::Cookbook> for examples. |
528 | |
7261aa9d |
529 | =head1 ATTRIBUTE INHERITANCE |
0b9488c8 |
530 | |
531 | By default, a child inherits all of its parent class(es)' attributes |
d4b96efa |
532 | as-is. However, you can explicitly change some aspects of the |
533 | inherited attribute in the child class. |
0b9488c8 |
534 | |
535 | The options that can be overridden in a subclass are: |
536 | |
537 | =over 4 |
538 | |
539 | =item * default |
540 | |
541 | =item * coerce |
542 | |
543 | =item * required |
544 | |
545 | =item * documentation |
546 | |
547 | =item * lazy |
548 | |
549 | =item * isa |
550 | |
551 | =item * handles |
552 | |
553 | =item * builder |
554 | |
555 | =item * metaclass |
556 | |
557 | =item * traits |
558 | |
559 | =back |
560 | |
561 | To override an attribute, you simply prepend its name with a plus sign |
0c39debe |
562 | (C<+>): |
0b9488c8 |
563 | |
564 | package LazyPerson; |
565 | |
566 | use Moose; |
567 | |
568 | extends 'Person'; |
569 | |
d983b81e |
570 | has '+first_name' => ( |
571 | lazy => 1, |
572 | default => 'Bill', |
573 | ); |
0b9488c8 |
574 | |
575 | Now the C<first_name> attribute in C<LazyPerson> is lazy, and defaults |
576 | to C<'Bill'>. |
577 | |
578 | We recommend that you exercise caution when changing the type (C<isa>) |
0c38f882 |
579 | of an inherited attribute. |
0b9488c8 |
580 | |
7261aa9d |
581 | =head1 MORE ON ATTRIBUTES |
582 | |
583 | Moose attributes are a big topic, and this document glosses over a few |
584 | aspects of their aspects. We recommend that you read the |
585 | L<Moose::Manual::Delegation> and L<Moose::Manual::Types> documents to |
586 | get a more complete understanding of attribute features. |
587 | |
588 | =head1 A FEW MORE OPTIONS |
589 | |
590 | Moose has lots of attribute options. The ones listed below are |
636f25f3 |
591 | superseded by some more modern features, but are covered for the sake |
7261aa9d |
592 | of completeness. |
593 | |
0b9488c8 |
594 | =head2 The C<documentation> option |
595 | |
596 | You can provide a piece of documentation as a string for an attribute: |
597 | |
d983b81e |
598 | has 'first_name' => ( |
599 | is => 'rw', |
600 | documentation => q{The person's first (personal) name}, |
601 | ); |
0b9488c8 |
602 | |
603 | Moose does absolutely nothing with this information other than store |
604 | it. |
605 | |
d67ce58f |
606 | =head2 The C<auto_deref> option |
0b9488c8 |
607 | |
608 | If your attribute is an array reference or hash reference, the |
636f25f3 |
609 | C<auto_deref> option will make Moose dereference the value when it is |
0b9488c8 |
610 | returned from the reader method: |
611 | |
612 | my %map = $object->mapping; |
613 | |
614 | This option only works if your attribute is explicitly typed as an |
0c39debe |
615 | C<ArrayRef> or C<HashRef>. |
0b9488c8 |
616 | |
0c39debe |
617 | However, we recommend that you use L<MooseX::AttributeHelpers> for |
0b9488c8 |
618 | these types of attributes, which gives you much more control over how |
619 | they are accessed and manipulated. |
620 | |
621 | =head2 Initializer |
622 | |
623 | Moose provides an attribute option called C<initializer>. This is |
624 | similar to C<builder>, except that it is I<only> called during object |
625 | construction. |
626 | |
0c39debe |
627 | This option is inherited from L<Class::MOP>, but we recommend that you |
0b9488c8 |
628 | use a C<builder> (which is Moose-only) instead. |
629 | |
d4b96efa |
630 | =head1 AUTHOR |
631 | |
632 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
633 | |
634 | =head1 COPYRIGHT AND LICENSE |
635 | |
2840a3b2 |
636 | Copyright 2009 by Infinity Interactive, Inc. |
d4b96efa |
637 | |
638 | L<http://www.iinteractive.com> |
639 | |
640 | This library is free software; you can redistribute it and/or modify |
641 | it under the same terms as Perl itself. |
642 | |
643 | =cut |