Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Moose::Manual::Attributes.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Moose::Manual::Attributes 3"
132 .TH Moose::Manual::Attributes 3 "2009-09-15" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 Moose::Manual::Attributes \- Object attributes with Moose
135 .SH "INTRODUCTION"
136 .IX Header "INTRODUCTION"
137 Moose attributes have many properties, and attributes are probably the
138 single most powerful and flexible part of Moose. You can create a
139 powerful class simply by declaring attributes. In fact, it's possible
140 to have classes that consist solely of attribute declarations.
141 .PP
142 An attribute is a property that every member of a class has. For
143 example, we might say that "every \f(CW\*(C`Person\*(C'\fR object has a first name and
144 last name\*(L". Attributes can be optional, so that we can say \*(R"some \f(CW\*(C`Person\*(C'\fR
145 objects have a social security number (and some don't)".
146 .PP
147 At its simplest, an attribute can be thought of as a named value (as
148 in a hash) that can be read and set. However, attributes can also have
149 defaults, type constraints, delegation and much more.
150 .PP
151 In other languages, attributes are also referred to as slots or
152 properties.
153 .SH "ATTRIBUTE OPTIONS"
154 .IX Header "ATTRIBUTE OPTIONS"
155 Use the \f(CW\*(C`has\*(C'\fR function to declare an attribute:
156 .PP
157 .Vb 1
158 \&  package Person;
159 .Ve
160 .PP
161 .Vb 1
162 \&  use Moose;
163 .Ve
164 .PP
165 .Vb 1
166 \&  has 'first_name' => ( is => 'rw' );
167 .Ve
168 .PP
169 This says that all \f(CW\*(C`Person\*(C'\fR objects have an optional read-write
170 \&\*(L"first_name\*(R" attribute.
171 .Sh "Read-write vs. read-only"
172 .IX Subsection "Read-write vs. read-only"
173 The options passed to \f(CW\*(C`has\*(C'\fR define the properties of the
174 attribute. There are many options, but in the simplest form you just
175 need to set \f(CW\*(C`is\*(C'\fR, which can be either \f(CW\*(C`rw\*(C'\fR (read\-write) or \f(CW\*(C`ro\*(C'\fR
176 (read\-only). When an attribute is \f(CW\*(C`rw\*(C'\fR, you can change it by passing
177 a value to its accessor. When an attribute is \f(CW\*(C`ro\*(C'\fR, you may only read
178 the current value of the attribute.
179 .PP
180 In fact, you could even omit \f(CW\*(C`is\*(C'\fR, but that gives you an attribute
181 that has no accessor. This can be useful with other attribute options,
182 such as \f(CW\*(C`handles\*(C'\fR. However, if your attribute generates \fIno\fR
183 accessors, Moose will issue a warning, because that usually means the
184 programmer forgot to say the attribute is read-only or read\-write. If
185 you really mean to have no accessors, you can silence this warning by
186 setting \f(CW\*(C`is\*(C'\fR to \f(CW\*(C`bare\*(C'\fR.
187 .Sh "Accessor methods"
188 .IX Subsection "Accessor methods"
189 Each attribute has one or more accessor methods. An accessor lets you
190 read and write the value of that attribute for an object.
191 .PP
192 By default, the accessor method has the same name as the attribute. If
193 you declared your attribute as \f(CW\*(C`ro\*(C'\fR then your accessor will be
194 read\-only. If you declared it read\-write, you get a read-write
195 accessor. Simple.
196 .PP
197 Given our \f(CW\*(C`Person\*(C'\fR example above, we now have a single \f(CW\*(C`first_name\*(C'\fR
198 accessor that can read or write a \f(CW\*(C`Person\*(C'\fR object's \f(CW\*(C`first_name\*(C'\fR
199 attribute's value.
200 .PP
201 If you want, you can also explicitly specify the method names to be
202 used for reading and writing an attribute's value. This is
203 particularly handy when you'd like an attribute to be publicly
204 readable, but only privately settable. For example:
205 .PP
206 .Vb 4
207 \&  has 'weight' => (
208 \&      is     => 'ro',
209 \&      writer => '_set_weight',
210 \&  );
211 .Ve
212 .PP
213 This might be useful if weight is calculated based on other methods.
214 For example, every time the \f(CW\*(C`eat\*(C'\fR method is called, we might adjust
215 weight. This lets us hide the implementation details of weight
216 changes, but still provide the weight value to users of the class.
217 .PP
218 Some people might prefer to have distinct methods for reading and
219 writing. In \fIPerl Best Practices\fR, Damian Conway recommends that
220 reader methods start with \*(L"get_\*(R" and writer methods start with \*(L"set_\*(R".
221 .PP
222 We can do exactly that by providing names for both the \f(CW\*(C`reader\*(C'\fR and
223 \&\f(CW\*(C`writer\*(C'\fR methods:
224 .PP
225 .Vb 5
226 \&  has 'weight' => (
227 \&      is     => 'rw',
228 \&      reader => 'get_weight',
229 \&      writer => 'set_weight',
230 \&  );
231 .Ve
232 .PP
233 If you're thinking that doing this over and over would be insanely
234 tedious, you're right! Fortunately, Moose provides a powerful
235 extension system that lets you override the default naming
236 conventions. See Moose::Manual::MooseX for more details.
237 .Sh "Predicate and clearer methods"
238 .IX Subsection "Predicate and clearer methods"
239 Moose allows you to explicitly distinguish between a false or
240 undefined attribute value and an attribute which has not been set. If
241 you want to access this information, you must define clearer and
242 predicate methods for an attribute.
243 .PP
244 A predicate method tells you whether or not a given attribute is
245 currently set. Note that an attribute can be explicitly set to
246 \&\f(CW\*(C`undef\*(C'\fR or some other false value, but the predicate will return
247 true.
248 .PP
249 The clearer method unsets the attribute. This is \fInot\fR the
250 same as setting the value to \f(CW\*(C`undef\*(C'\fR, but you can only distinguish
251 between them if you define a predicate method!
252 .PP
253 Here's some code to illustrate the relationship between an accessor,
254 predicate, and clearer method.
255 .PP
256 .Vb 1
257 \&  package Person;
258 .Ve
259 .PP
260 .Vb 1
261 \&  use Moose;
262 .Ve
263 .PP
264 .Vb 5
265 \&  has 'ssn' => (
266 \&      is        => 'rw',
267 \&      clearer   => 'clear_ssn',
268 \&      predicate => 'has_ssn',
269 \&  );
270 .Ve
271 .PP
272 .Vb 1
273 \&  ...
274 .Ve
275 .PP
276 .Vb 2
277 \&  my $person = Person\->new();
278 \&  $person\->has_ssn; # false
279 .Ve
280 .PP
281 .Vb 3
282 \&  $person\->ssn(undef);
283 \&  $person\->ssn; # returns undef
284 \&  $person\->has_ssn; # true
285 .Ve
286 .PP
287 .Vb 3
288 \&  $person\->clear_ssn;
289 \&  $person\->ssn; # returns undef
290 \&  $person\->has_ssn; # false
291 .Ve
292 .PP
293 .Vb 3
294 \&  $person\->ssn('123\-45\-6789');
295 \&  $person\->ssn; # returns '123\-45\-6789'
296 \&  $person\->has_ssn; # true
297 .Ve
298 .PP
299 .Vb 2
300 \&  my $person2 = Person\->new( ssn => '111\-22\-3333');
301 \&  $person2\->has_ssn; # true
302 .Ve
303 .PP
304 By default, Moose does not make a predicate or clearer for you. You must
305 explicitly provide names for them, and then Moose will create the methods
306 for you.
307 .Sh "Required or not?"
308 .IX Subsection "Required or not?"
309 By default, all attributes are optional, and do not need to be
310 provided at object construction time. If you want to make an attribute
311 required, simply set the \f(CW\*(C`required\*(C'\fR option to true:
312 .PP
313 .Vb 4
314 \&  has 'name' => (
315 \&      is       => 'ro',
316 \&      required => 1,
317 \&  );
318 .Ve
319 .PP
320 There are a couple caveats worth mentioning in regards to what
321 \&\*(L"required\*(R" actually means.
322 .PP
323 Basically, all it says is that this attribute (\f(CW\*(C`name\*(C'\fR) must be provided to
324 the constructor, or be lazy with either a default or a builder. It does not
325 say anything about its value, so it could be \f(CW\*(C`undef\*(C'\fR.
326 .PP
327 If you define a clearer method on a required attribute, the clearer
328 \&\fIwill\fR work, so even a required attribute can be unset after object
329 construction.
330 .PP
331 This means that if you do make an attribute required, providing a
332 clearer doesn't make much sense. In some cases, it might be handy to
333 have a \fIprivate\fR \f(CW\*(C`clearer\*(C'\fR and \f(CW\*(C`predicate\*(C'\fR for a required
334 attribute.
335 .Sh "Default and builder methods"
336 .IX Subsection "Default and builder methods"
337 Attributes can have default values, and Moose provides two ways to
338 specify that default.
339 .PP
340 In the simplest form, you simply provide a non-reference scalar value
341 for the \f(CW\*(C`default\*(C'\fR option:
342 .PP
343 .Vb 5
344 \&  has 'size' => (
345 \&      is        => 'ro',
346 \&      default   => 'medium',
347 \&      predicate => 'has_size',
348 \&  );
349 .Ve
350 .PP
351 If the size attribute is not provided to the constructor, then it ends
352 up being set to \f(CW\*(C`medium\*(C'\fR:
353 .PP
354 .Vb 3
355 \&  my $person = Person\->new();
356 \&  $person\->size; # medium
357 \&  $person\->has_size; # true
358 .Ve
359 .PP
360 You can also provide a subroutine reference for \f(CW\*(C`default\*(C'\fR. This
361 reference will be called as a method on the object.
362 .PP
363 .Vb 6
364 \&  has 'size' => (
365 \&      is => 'ro',
366 \&      default =>
367 \&          sub { ( 'small', 'medium', 'large' )[ int( rand 3 ) ] },
368 \&      predicate => 'has_size',
369 \&  );
370 .Ve
371 .PP
372 This is a dumb example, but it illustrates the point that the subroutine
373 will be called for every new object created.
374 .PP
375 When you provide a \f(CW\*(C`default\*(C'\fR subroutine reference, it is called as a
376 method on the object, with no additional parameters:
377 .PP
378 .Vb 4
379 \&  has 'size' => (
380 \&      is => 'ro',
381 \&      default => sub {
382 \&          my $self = shift;
383 .Ve
384 .PP
385 .Vb 3
386 \&          return $self\->height > 200 ? 'big' : 'average';
387 \&      },
388 \&  );
389 .Ve
390 .PP
391 When the \f(CW\*(C`default\*(C'\fR is called during object construction, it may be
392 called before other attributes have been set. If your default is
393 dependent on other parts of the object's state, you can make the
394 attribute \f(CW\*(C`lazy\*(C'\fR. Laziness is covered in the next section.
395 .PP
396 If you want to use a reference of any sort as the default value, you
397 must return it from a subroutine. This is necessary because otherwise
398 Perl would instantiate the reference exactly once, and it would be
399 shared by all objects:
400 .PP
401 .Vb 4
402 \&  has 'mapping' => (
403 \&      is      => 'ro',
404 \&      default => {}, # wrong!
405 \&  );
406 .Ve
407 .PP
408 Moose will throw an error if you pass a bare non-subroutine reference
409 as the default.
410 .PP
411 If Moose allowed this then the default mapping attribute could easily
412 end up shared across many objects. Instead, wrap it in a subroutine
413 reference:
414 .PP
415 .Vb 4
416 \&  has 'mapping' => (
417 \&      is      => 'ro',
418 \&      default => sub { {} }, # right!
419 \&  );
420 .Ve
421 .PP
422 This is a bit awkward, but it's just the way Perl works.
423 .PP
424 As an alternative to using a subroutine reference, you can instead
425 supply a \f(CW\*(C`builder\*(C'\fR method for your attribute:
426 .PP
427 .Vb 5
428 \&  has 'size' => (
429 \&      is        => 'ro',
430 \&      builder   => '_build_size',
431 \&      predicate => 'has_size',
432 \&  );
433 .Ve
434 .PP
435 .Vb 3
436 \&  sub _build_size {
437 \&      return ( 'small', 'medium', 'large' )[ int( rand 3 ) ];
438 \&  }
439 .Ve
440 .PP
441 This has several advantages. First, it moves a chunk of code to its
442 own named method, which improves readability and code
443 organization.
444 .PP
445 We strongly recommend that you use a \f(CW\*(C`builder\*(C'\fR instead of a
446 \&\f(CW\*(C`default\*(C'\fR for anything beyond the most trivial default.
447 .PP
448 A \f(CW\*(C`builder\*(C'\fR, just like a \f(CW\*(C`default\*(C'\fR, is called as a method on the
449 object with no additional parameters.
450 .PP
451 \fIBuilders allow subclassing\fR
452 .IX Subsection "Builders allow subclassing"
453 .PP
454 Because the \f(CW\*(C`builder\*(C'\fR is called \fIby name\fR, it goes through Perl's
455 method resolution. This means that builder methods are both
456 inheritable and overridable.
457 .PP
458 If we subclass our \f(CW\*(C`Person\*(C'\fR class, we can override \f(CW\*(C`_build_size\*(C'\fR:
459 .PP
460 .Vb 1
461 \&  package Lilliputian;
462 .Ve
463 .PP
464 .Vb 2
465 \&  use Moose;
466 \&  extends 'Person';
467 .Ve
468 .PP
469 .Vb 1
470 \&  sub _build_size { return 'small' }
471 .Ve
472 .PP
473 \fIBuilders can be composed from roles\fR
474 .IX Subsection "Builders can be composed from roles"
475 .PP
476 Because builders are called by name, they work well with roles. For
477 example, a role could provide an attribute but require that the
478 consuming class provide the \f(CW\*(C`builder\*(C'\fR:
479 .PP
480 .Vb 2
481 \&  package HasSize;
482 \&  use Moose::Role;
483 .Ve
484 .PP
485 .Vb 1
486 \&  requires '_build_size';
487 .Ve
488 .PP
489 .Vb 5
490 \&  has 'size' => (
491 \&      is      => 'ro',
492 \&      lazy    => 1,
493 \&      builder => '_build_size',
494 \&  );
495 .Ve
496 .PP
497 .Vb 2
498 \&  package Lilliputian;
499 \&  use Moose;
500 .Ve
501 .PP
502 .Vb 1
503 \&  with 'HasSize';
504 .Ve
505 .PP
506 .Vb 1
507 \&  sub _build_size { return 'small' }
508 .Ve
509 .PP
510 Roles are covered in Moose::Manual::Roles.
511 .ie n .Sh "Laziness and ""lazy_build"""
512 .el .Sh "Laziness and \f(CWlazy_build\fP"
513 .IX Subsection "Laziness and lazy_build"
514 Moose lets you defer attribute population by making an attribute
515 \&\f(CW\*(C`lazy\*(C'\fR:
516 .PP
517 .Vb 5
518 \&  has 'size' => (
519 \&      is      => 'ro',
520 \&      lazy    => 1,
521 \&      builder => '_build_size',
522 \&  );
523 .Ve
524 .PP
525 When \f(CW\*(C`lazy\*(C'\fR is true, the default is not generated until the reader
526 method is called, rather than at object construction time. There are
527 several reasons you might choose to do this.
528 .PP
529 First, if the default value for this attribute depends on some other
530 attributes, then the attribute \fImust\fR be \f(CW\*(C`lazy\*(C'\fR. During object
531 construction, defaults are not generated in a predictable order, so
532 you cannot count on some other attribute being populated when
533 generating a default.
534 .PP
535 Second, there's often no reason to calculate a default before it's
536 needed. Making an attribute \f(CW\*(C`lazy\*(C'\fR lets you defer the cost until the
537 attribute is needed. If the attribute is \fInever\fR needed, you save
538 some \s-1CPU\s0 time.
539 .PP
540 We recommend that you make any attribute with a builder or non-trivial
541 default \f(CW\*(C`lazy\*(C'\fR as a matter of course.
542 .PP
543 To facilitate this, you can simply specify the \f(CW\*(C`lazy_build\*(C'\fR attribute
544 option. This bundles up a number of options together:
545 .PP
546 .Vb 4
547 \&  has 'size' => (
548 \&      is         => 'ro',
549 \&      lazy_build => 1,
550 \&  );
551 .Ve
552 .PP
553 This is the same as specifying all of these options:
554 .PP
555 .Vb 7
556 \&  has 'size' => (
557 \&      is        => 'ro',
558 \&      lazy      => 1,
559 \&      builder   => '_build_size',
560 \&      clearer   => 'clear_size',
561 \&      predicate => 'has_size',
562 \&  );
563 .Ve
564 .PP
565 If your attribute name starts with an underscore (\f(CW\*(C`_\*(C'\fR), then the clearer
566 and predicate will as well:
567 .PP
568 .Vb 4
569 \&  has '_size' => (
570 \&      is         => 'ro',
571 \&      lazy_build => 1,
572 \&  );
573 .Ve
574 .PP
575 becomes:
576 .PP
577 .Vb 7
578 \&  has '_size' => (
579 \&      is        => 'ro',
580 \&      lazy      => 1,
581 \&      builder   => '_build__size',
582 \&      clearer   => '_clear_size',
583 \&      predicate => '_has_size',
584 \&  );
585 .Ve
586 .PP
587 Note the doubled underscore in the builder name. Internally, Moose
588 simply prepends the attribute name with \*(L"_build_\*(R" to come up with the
589 builder name.
590 .PP
591 If you don't like the names that \f(CW\*(C`lazy_build\*(C'\fR generates, you can
592 always provide your own:
593 .PP
594 .Vb 5
595 \&  has 'size' => (
596 \&      is         => 'ro',
597 \&      lazy_build => 1,
598 \&      clearer    => '_clear_size',
599 \&  );
600 .Ve
601 .PP
602 Options that you explicitly provide are always used in favor of
603 Moose's internal defaults.
604 .ie n .Sh "Constructor parameters (""init_arg"")"
605 .el .Sh "Constructor parameters (\f(CWinit_arg\fP)"
606 .IX Subsection "Constructor parameters (init_arg)"
607 By default, each attribute can be passed by name to the class's
608 constructor. On occasion, you may want to use a different name for
609 the constructor parameter. You may also want to make an attribute
610 unsettable via the constructor.
611 .PP
612 Both of these goals can be accomplished with the \f(CW\*(C`init_arg\*(C'\fR option:
613 .PP
614 .Vb 4
615 \&  has 'bigness' => (
616 \&      is       => 'ro',
617 \&      init_arg => 'size',
618 \&  );
619 .Ve
620 .PP
621 Now we have an attribute named \*(L"bigness\*(R", but we pass \f(CW\*(C`size\*(C'\fR to the
622 constructor.
623 .PP
624 Even more useful is the ability to disable setting an attribute via
625 the constructor. This is particularly handy for private attributes:
626 .PP
627 .Vb 5
628 \&  has '_genetic_code' => (
629 \&      is         => 'ro',
630 \&      lazy_build => 1,
631 \&      init_arg   => undef,
632 \&  );
633 .Ve
634 .PP
635 By setting the \f(CW\*(C`init_arg\*(C'\fR to \f(CW\*(C`undef\*(C'\fR, we make it impossible to set
636 this attribute when creating a new object.
637 .Sh "Weak references"
638 .IX Subsection "Weak references"
639 Moose has built-in support for weak references. If you set the
640 \&\f(CW\*(C`weak_ref\*(C'\fR option to a true value, then it will call
641 \&\f(CW\*(C`Scalar::Util::weaken\*(C'\fR whenever the attribute is set:
642 .PP
643 .Vb 4
644 \&  has 'parent' => (
645 \&      is       => 'rw',
646 \&      weak_ref => 1,
647 \&  );
648 .Ve
649 .PP
650 .Vb 1
651 \&  $node\->parent($parent_node);
652 .Ve
653 .PP
654 This is very useful when you're building objects that may contain
655 circular references.
656 .Sh "Triggers"
657 .IX Subsection "Triggers"
658 A \f(CW\*(C`trigger\*(C'\fR is a subroutine that is called whenever the attribute is
659 set:
660 .PP
661 .Vb 4
662 \&  has 'size' => (
663 \&      is      => 'rw',
664 \&      trigger => \e&_size_set,
665 \&  );
666 .Ve
667 .PP
668 .Vb 2
669 \&  sub _size_set {
670 \&      my ( $self, $size, $old_size ) = @_;
671 .Ve
672 .PP
673 .Vb 1
674 \&      my $msg = $self\->name;
675 .Ve
676 .PP
677 .Vb 3
678 \&      if ( @_ > 2 ) {
679 \&          $msg .= " \- old size was $old_size";
680 \&      }
681 .Ve
682 .PP
683 .Vb 3
684 \&      $msg .= " \- size is now $size";
685 \&      warn $msg.
686 \&  }
687 .Ve
688 .PP
689 The trigger is called \fIafter\fR an attribute's value is set. It is
690 called as a method on the object, and receives the new and values as
691 its arguments. If the attribute had not previously been set at all,
692 then only the new value is passed. This lets you distinguish between
693 the case where the attribute had no value versus when it was \f(CW\*(C`undef\*(C'\fR.
694 .PP
695 This differs from an \f(CW\*(C`after\*(C'\fR method modifier in two ways. First, a
696 trigger is only called when the attribute is set, as opposed to
697 whenever the accessor method is called (for reading or
698 writing). Second, it is also called when an attribute's value is
699 passed to the constructor.
700 .PP
701 However, triggers are \fInot\fR called when an attribute is populated
702 from a \f(CW\*(C`default\*(C'\fR or \f(CW\*(C`builder\*(C'\fR
703 .Sh "Attribute types"
704 .IX Subsection "Attribute types"
705 Attributes can be restricted to only accept certain types:
706 .PP
707 .Vb 4
708 \&  has 'first_name' => (
709 \&      is  => 'ro',
710 \&      isa => 'Str',
711 \&  );
712 .Ve
713 .PP
714 This says that the \f(CW\*(C`first_name\*(C'\fR attribute must be a string.
715 .PP
716 Moose also provides a shortcut for specifying that an attribute only
717 accepts objects that do a certain role:
718 .PP
719 .Vb 4
720 \&  has 'weapon' => (
721 \&      is   => 'rw',
722 \&      does => 'MyApp::Weapon',
723 \&  );
724 .Ve
725 .PP
726 See the Moose::Manual::Types documentation for a complete
727 discussion of Moose's type system.
728 .Sh "Delegation"
729 .IX Subsection "Delegation"
730 An attribute can define methods which simply delegate to its value:
731 .PP
732 .Vb 5
733 \&  has 'hair_color' => (
734 \&      is      => 'ro',
735 \&      isa     => 'Graphics::Color::RGB',
736 \&      handles => { hair_color_hex => 'as_hex_string' },
737 \&  );
738 .Ve
739 .PP
740 This adds a new method, \f(CW\*(C`hair_color_hex\*(C'\fR. When someone calls
741 \&\f(CW\*(C`hair_color_hex\*(C'\fR, internally, the object just calls \f(CW\*(C`$self\->hair_color\->as_hex_string\*(C'\fR.
742 .PP
743 See Moose::Manual::Delegation for documentation on how to set up
744 delegation methods.
745 .Sh "Metaclass and traits"
746 .IX Subsection "Metaclass and traits"
747 One of Moose's best features is that it can be extended in all sorts
748 of ways through the use of custom metaclasses and metaclass traits.
749 .PP
750 When declaring an attribute, you can declare a metaclass or a set of
751 traits for the attribute:
752 .PP
753 .Vb 5
754 \&  has 'mapping' => (
755 \&      metaclass => 'Hash',
756 \&      is        => 'ro',
757 \&      default   => sub { {} },
758 \&  );
759 .Ve
760 .PP
761 In this case, the metaclass \f(CW\*(C`Hash\*(C'\fR really refers to
762 Moose::Meta::Attribute::Native::Trait::Hash. Moose also provides
763 native traits for Number, 
764 Counter, 
765 String, 
766 Bool, and 
767 Array.
768 .PP
769 You can also apply one or more traits to an attribute:
770 .PP
771 .Vb 1
772 \&  use MooseX::MetaDescription;
773 .Ve
774 .PP
775 .Vb 8
776 \&  has 'size' => (
777 \&      is          => 'ro',
778 \&      traits      => ['MooseX::MetaDescription::Meta::Trait'],
779 \&      description => {
780 \&          html_widget  => 'text_input',
781 \&          serialize_as => 'element',
782 \&      },
783 \&  );
784 .Ve
785 .PP
786 The advantage of traits is that you can mix more than one of them
787 together easily (in fact, a trait is just a role under the hood).
788 .PP
789 There are a number of MooseX modules on \s-1CPAN\s0 which provide useful
790 attribute metaclasses and traits. See Moose::Manual::MooseX for
791 some examples. You can also write your own metaclasses and traits. See
792 the \*(L"Meta\*(R" and \*(L"Extending\*(R" recipes in Moose::Cookbook for examples.
793 .SH "ATTRIBUTE INHERITANCE"
794 .IX Header "ATTRIBUTE INHERITANCE"
795 By default, a child inherits all of its parent class(es)' attributes
796 as\-is. However, you can explicitly change some aspects of the
797 inherited attribute in the child class.
798 .PP
799 The options that can be overridden in a subclass are:
800 .IP "* default" 4
801 .IX Item "default"
802 .PD 0
803 .IP "* coerce" 4
804 .IX Item "coerce"
805 .IP "* required" 4
806 .IX Item "required"
807 .IP "* documentation" 4
808 .IX Item "documentation"
809 .IP "* lazy" 4
810 .IX Item "lazy"
811 .IP "* isa" 4
812 .IX Item "isa"
813 .IP "* handles" 4
814 .IX Item "handles"
815 .IP "* builder" 4
816 .IX Item "builder"
817 .IP "* metaclass" 4
818 .IX Item "metaclass"
819 .IP "* traits" 4
820 .IX Item "traits"
821 .PD
822 .PP
823 To override an attribute, you simply prepend its name with a plus sign
824 (\f(CW\*(C`+\*(C'\fR):
825 .PP
826 .Vb 1
827 \&  package LazyPerson;
828 .Ve
829 .PP
830 .Vb 1
831 \&  use Moose;
832 .Ve
833 .PP
834 .Vb 1
835 \&  extends 'Person';
836 .Ve
837 .PP
838 .Vb 4
839 \&  has '+first_name' => (
840 \&      lazy    => 1,
841 \&      default => 'Bill',
842 \&  );
843 .Ve
844 .PP
845 Now the \f(CW\*(C`first_name\*(C'\fR attribute in \f(CW\*(C`LazyPerson\*(C'\fR is lazy, and defaults
846 to \f(CW'Bill'\fR.
847 .PP
848 We recommend that you exercise caution when changing the type (\f(CW\*(C`isa\*(C'\fR)
849 of an inherited attribute.
850 .SH "MULTIPLE ATTRIBUTE SHORTCUTS"
851 .IX Header "MULTIPLE ATTRIBUTE SHORTCUTS"
852 If you have a number of attributes that differ only be name, you can declare
853 them all at once:
854 .PP
855 .Vb 1
856 \&  package Point;
857 .Ve
858 .PP
859 .Vb 1
860 \&  use Moose;
861 .Ve
862 .PP
863 .Vb 1
864 \&  has [ 'x', 'y' ] => ( is => 'ro', isa => 'Int' );
865 .Ve
866 .PP
867 Also, because \f(CW\*(C`has\*(C'\fR is just a function call, you can call it in a loop:
868 .PP
869 .Vb 4
870 \&  for my $name ( qw( x y ) ) {
871 \&      my $builder = '_build_' . $name;
872 \&      has $name => ( is => 'ro', isa => 'Int', builder => $builder );
873 \&  }
874 .Ve
875 .SH "MORE ON ATTRIBUTES"
876 .IX Header "MORE ON ATTRIBUTES"
877 Moose attributes are a big topic, and this document glosses over a few
878 aspects. We recommend that you read the Moose::Manual::Delegation
879 and Moose::Manual::Types documents to get a more complete
880 understanding of attribute features.
881 .SH "A FEW MORE OPTIONS"
882 .IX Header "A FEW MORE OPTIONS"
883 Moose has lots of attribute options. The ones listed below are
884 superseded by some more modern features, but are covered for the sake
885 of completeness.
886 .ie n .Sh "The ""documentation"" option"
887 .el .Sh "The \f(CWdocumentation\fP option"
888 .IX Subsection "The documentation option"
889 You can provide a piece of documentation as a string for an attribute:
890 .PP
891 .Vb 4
892 \&  has 'first_name' => (
893 \&      is            => 'rw',
894 \&      documentation => q{The person's first (personal) name},
895 \&  );
896 .Ve
897 .PP
898 Moose does absolutely nothing with this information other than store
899 it.
900 .ie n .Sh "The ""auto_deref"" option"
901 .el .Sh "The \f(CWauto_deref\fP option"
902 .IX Subsection "The auto_deref option"
903 If your attribute is an array reference or hash reference, the
904 \&\f(CW\*(C`auto_deref\*(C'\fR option will make Moose dereference the value when it is
905 returned from the reader method:
906 .PP
907 .Vb 1
908 \&  my %map = $object\->mapping;
909 .Ve
910 .PP
911 This option only works if your attribute is explicitly typed as an
912 \&\f(CW\*(C`ArrayRef\*(C'\fR or \f(CW\*(C`HashRef\*(C'\fR.
913 .PP
914 However, we recommend that you use Moose::Meta::Attribute::Native traits
915 for these types of attributes, which gives you much more control over how
916 they are accessed and manipulated.
917 .Sh "Initializer"
918 .IX Subsection "Initializer"
919 Moose provides an attribute option called \f(CW\*(C`initializer\*(C'\fR. This is
920 similar to \f(CW\*(C`builder\*(C'\fR, except that it is \fIonly\fR called during object
921 construction.
922 .PP
923 This option is inherited from Class::MOP, but we recommend that you
924 use a \f(CW\*(C`builder\*(C'\fR (which is Moose\-only) instead.
925 .SH "AUTHOR"
926 .IX Header "AUTHOR"
927 Dave Rolsky <autarch@urth.org>
928 .SH "COPYRIGHT AND LICENSE"
929 .IX Header "COPYRIGHT AND LICENSE"
930 Copyright 2009 by Infinity Interactive, Inc.
931 .PP
932 <http://www.iinteractive.com>
933 .PP
934 This library is free software; you can redistribute it and/or modify
935 it under the same terms as Perl itself.