bump version to 0.66
[gitmo/Moose.git] / lib / Moose / Manual / Attributes.pod
index 854d9e8..18f0f2e 100644 (file)
@@ -6,12 +6,12 @@ Moose::Manual::Attribute - Object attributes with Moose
 
 =head1 INTRODUCTION
 
-Moose has many attribute-related features, and attributes are probably
-the single most useful aspect of Moose. You can do a lot in a class
-just by declaring attributes. In fact, it's quite possible to have
-classes that consist solely of attribute declarations.
+Moose attributes have many properties, and attributes are probably the
+single most powerful and flexible part of Moose. You can create a
+powerful class simply by declaring attributes. In fact, it's possible
+to have classes that consist solely of attribute declarations.
 
-An Attribute is a property that every member of a class has. For
+An attribute is a property that every member of a class has. For
 example, we might say that "every Person object has a first name and
 last name". Attributes can be optional, so that we can say "some Person
 objects have a social security number (and some don't)".
@@ -20,6 +20,9 @@ At its simplest, an attribute can be thought of as a named value (as
 in a hash) that can be read and set. However, attributes can also have
 defaults, type constraints, delegation and much more.
 
+In other languages, attributes are also referred to as slots or
+properties.
+
 =head1 ATTRIBUTE OPTIONS
 
 Use the C<has> function to declare an attribute:
@@ -35,14 +38,14 @@ This says that all person objects have an optional read-write
 
 =head2 Read-write Vs Read-only
 
-The options passed to C<has> define the details of the
-attribute. There are a lot of options you can put here, but in the
-simplest form you just need to include C<is>, which can be either
-C<rw> (read-write) or C<ro> (read-only).
+The options passed to C<has> define the properties of the
+attribute. There are a many options, but in the simplest form you just
+need to set C<is>, which can be either C<rw> (read-write) or C<ro>
+(read-only).
 
-(In fact, you could even omit C<is>, but that leaves you with an
-attribute that has no accessors, which is pointless unless you're
-doing some deep, dark magic).
+(In fact, you could even omit C<is>, but that gives you an attribute
+that has no accessors, which is pointless unless you're doing some
+deep, dark magic).
 
 =head2 Accessor Methods
 
@@ -62,10 +65,10 @@ used for reading and writing an attribute's value. This is
 particularly handy when you'd like an attribute to be publically
 readable, but only privately settable. For example:
 
-  has 'weight' =>
-      ( is     => 'rw',
-        writer => '_set_weight',
-      );
+  has 'weight' => (
+      is     => 'rw',
+      writer => '_set_weight',
+  );
 
 This might be useful if weight is calculated based on other methods,
 for example every time the C<eat> method is called, we might adjust
@@ -79,28 +82,27 @@ reader methods start with "get_" and writer methods start with "set_".
 We can do exactly that by providing names for both the C<reader> and
 C<writer> methods:
 
-  has 'weight' =>
-      ( is     => 'rw',
-        reader => 'get_weight',
-        writer => 'set_weight',
-      );
+  has 'weight' => (
+      is     => 'rw',
+      reader => 'get_weight',
+      writer => 'set_weight',
+  );
 
 If you're thinking that doing this over and over would be insanely
 tedious, you're right! Fortunately, Moose provides a powerful
-extension system that lets you do things like override the default
-accessor method conventions. See L<Moose::Manual::MooseX> for more
-details.
+extension system that lets override the default naming
+conventions. See L<Moose::Manual::MooseX> for more details.
 
 =head2 Predicate and Clearer Methods
 
 Moose allows you to explicitly distinguish between a false or
-undefined attribute value and an attribute which is not set. If you
-want to be able access this information, you must define clearer and
+undefined attribute value and an attribute which has not been set. If
+you want to access this information, you must define clearer and
 predicate methods for an attribute.
 
 A predicate method tells you whether or not a given attribute is
-currently set. Note that even if the attribute was explicitly set to
-undef or some other false value, the predicate will return true.
+currently set. Note an attribute can be explicitly set to undef or
+some other false value, but the predicate will return true.
 
 The clearer method unsets the attribute. This is I<not> the
 same as setting the value to C<undef>, but you can only distinguish
@@ -113,11 +115,11 @@ predicate, and clearer method.
 
   use Moose;
 
-  has 'ssn' =>
-      ( is        => 'rw',
-        clearer   => 'clear_ssn',
-        predicate => 'has_ssn',
-      );
+  has 'ssn' => (
+      is        => 'rw',
+      clearer   => 'clear_ssn',
+      predicate => 'has_ssn',
+  );
 
   ...
 
@@ -140,8 +142,7 @@ predicate, and clearer method.
   $person2->has_ssn; # true
 
 By default, Moose does not make a predicate or clearer for you. You
-must explicitly provide method names for these options if you want
-them.
+must explicitly provide names for them.
 
 =head2 Required or Not?
 
@@ -149,10 +150,10 @@ By default, all attributes are optional, and do not need to be
 provided at object construction time. If you want to make an attribute
 required, simply set the C<required> option to true:
 
-  has 'name' =>
-      ( is       => 'rw',
-        required => 1,
-      );
+  has 'name' => (
+      is       => 'rw',
+      required => 1,
+  );
 
 There are a couple caveats worth mentioning in regards to what
 required actually means.
@@ -178,11 +179,11 @@ specify that default.
 In the simplest form, you simply provide a non-reference scalar value
 for the C<default> option:
 
-  has 'size' =>
-      ( is        => 'rw',
-        default   => 'medium',
-        predicate => 'has_size',
-      );
+  has 'size' => (
+      is        => 'rw',
+      default   => 'medium',
+      predicate => 'has_size',
+  );
 
 If the size attribute is not provided to the constructor, then it ends
 up being set to "medium":
@@ -192,32 +193,32 @@ up being set to "medium":
   $person->has_size; # true
 
 You can also provide a subroutine reference for C<default>. This
-reference will be called a method on the object.
+reference will be called as a method on the object.
 
-  has 'size' =>
-      ( is        => 'rw',
-        default   =>
-            sub { ('small', 'medium', 'large')[ int( rand 3 ) ] },
-        predicate => 'has_size',
-      );
+  has 'size' => (
+      is => 'rw',
+      default =>
+          sub { ( 'small', 'medium', 'large' )[ int( rand 3 ) ] },
+      predicate => 'has_size',
+  );
 
 This is dumb example, but it illustrates the point that the subroutine
 will be called for every new object created.
 
 Of course, if it's called during object construction, it may be called
 before other attributes have been set. If your default is dependent on
-other parts of the object's state, you can make the default c<lazy>,
-which is covered in the next section.
+other parts of the object's state, you can make the attribute
+C<lazy>. Laziness is covered in the next section.
 
 If you want to use a reference of any sort as the default value, you
 must return it from a subroutine. This is necessary because otherwise
 Perl would instantiate the reference exactly once, and it would be
 shared by all objects:
 
-  has 'mapping' =>
-      ( is      => 'rw',
-        default => {}, # wrong!
-      );
+  has 'mapping' => (
+      is      => 'rw',
+      default => {}, # wrong!
+  );
 
 Moose will throw an error if you pass a bare non-subroutine reference
 as the default.
@@ -226,24 +227,24 @@ If Moose allowed this then the default mapping attribute could easily
 end up shared across many objects. Instead, wrap it in a subroutine
 reference:
 
-  has 'mapping' =>
-      ( is      => 'rw',
-        default => sub { {} }, # right!
-      );
+  has 'mapping' => (
+      is      => 'rw',
+      default => sub { {} }, # right!
+  );
 
 This is a bit awkward, but it's just the way Perl works.
 
 As an alternative to using a subroutine reference, you can instead
 supply a C<builder> method for your attribute:
 
-  has 'size' =>
-      ( is        => 'rw',
-        builder   => '_build_size',
-        predicate => 'has_size',
-      );
+  has 'size' => (
+      is        => 'rw',
+      builder   => '_build_size',
+      predicate => 'has_size',
+  );
 
   sub _build_size {
-      return ('small', 'medium', 'large')[ int( rand 3 ) ];
+      return ( 'small', 'medium', 'large' )[ int( rand 3 ) ];
   }
 
 This has several advantages. First, it moves a chunk of code to its
@@ -259,21 +260,21 @@ C<default> for anything beyond the most trivial default.
 Moose lets you defer attribute population by making an attribute
 C<lazy>:
 
-  has 'size' =>
-      ( is        => 'rw',
-        lazy      => 1,
-        builder   => '_build_size',
-      );
+  has 'size' => (
+      is      => 'rw',
+      lazy    => 1,
+      builder => '_build_size',
+  );
 
-When C<lazy> is true, the attribute is not populated until the reader
+When C<lazy> is true, the default is not generated until the reader
 method is called, rather than at object construction time. There are
 several reasons you might choose to do this.
 
 First, if the default value for this attribute depends on some other
 attributes, then the attribute I<must> be C<lazy>. During object
 construction, defaults are not generated in a predictable order, so
-you cannot count on some other attribute being populated in a non-lazy
-default subroutine.
+you cannot count on some other attribute being populated when
+generating a default.
 
 Second, there's often no reason to calculate a default before it's
 needed. Making an attribute C<lazy> lets you defer the cost until the
@@ -286,38 +287,38 @@ default C<lazy> as a matter of course.
 To facilitate this, you can simply specify the C<lazy_build> attribute
 option. This bundles up a number of options together:
 
-  has 'size' =>
-      ( is         => 'rw',
-        lazy_build => 1,
-      );
+  has 'size' => (
+      is         => 'rw',
+      lazy_build => 1,
+  );
 
 This is the same as specifying all of these options:
 
-  has 'size' =>
-      ( is        => 'rw',
-        lazy      => 1,
-        builder   => '_build_size',
-        clearer   => 'clear_size',
-        predicate => 'has_size',
-      );
+  has 'size' => (
+      is        => 'rw',
+      lazy      => 1,
+      builder   => '_build_size',
+      clearer   => 'clear_size',
+      predicate => 'has_size',
+  );
 
 If your attribute name starts with an underscore (_), then the clearer
 and predicate will as well:
 
-  has '_size' =>
-      ( is         => 'rw',
-        lazy_build => 1,
-      );
+  has '_size' => (
+      is         => 'rw',
+      lazy_build => 1,
+  );
 
 becomes:
 
-  has '_size' =>
-      ( is        => 'rw',
-        lazy      => 1,
-        builder   => '_build__size',
-        clearer   => '_clear_size',
-        predicate => '_has_size',
-      );
+  has '_size' => (
+      is        => 'rw',
+      lazy      => 1,
+      builder   => '_build__size',
+      clearer   => '_clear_size',
+      predicate => '_has_size',
+  );
 
 Note the doubled underscore in the builder name. Internally, Moose
 simply prepends the attribute name with "_build_" to come up with the
@@ -326,11 +327,11 @@ builder name.
 If you don't like the names that C<lazy_build> generates, you can
 always provide your own:
 
-  has 'size' =>
-      ( is         => 'rw',
-        lazy_build => 1,
-        clearer    => '_clear_size',
-      );
+  has 'size' => (
+      is         => 'rw',
+      lazy_build => 1,
+      clearer    => '_clear_size',
+  );
 
 Options that you explicitly provide are always used in favor of
 Moose's internal defaults.
@@ -340,26 +341,26 @@ Moose's internal defaults.
 By default, each attribute can be passed by name to the class's
 constructor. On occassion, you may want to use a different name for
 the constructor parameter. You may also want to make an attribute
-unsettable from the constructor.
+unsettable via the constructor.
 
-Both of these things can be done by providing a value for the
-C<init_arg> option:
+Both of these goals can be accomplished with the C<init_arg> option:
 
-  has 'bigness' =>
-      ( is       => 'rw',
-        init_arg => 'size',
-      );
+  has 'bigness' => (
+      is       => 'rw',
+      init_arg => 'size',
+  );
 
-Now we have an attribute named bigness, but to set it during object
-construction we pass C<size> to the constructor.
+Now we have an attribute named bigness, but we pass C<size> to the
+constructor.
 
-Even more useful is the ability to disable setting attribute. This is
-particularly handy for private attributes:
+Even more useful is the ability to disable setting an attribute via
+the constructor. This is particularly handy for private attributes:
 
-  has '_genetic_code' =>
-     ( is       => 'rw',
-       init_arg => undef,
-     );
+  has '_genetic_code' => (
+      is         => 'rw',
+      lazy_build => 1,
+      init_arg   => undef,
+  );
 
 By setting the C<init_arg> to C<undef>, we make it impossible to set
 this attribute when creating a new object.
@@ -370,10 +371,10 @@ Moose has built-in support for weak references. If you set the
 C<weak_ref> option to a true value, then it will call
 C<Scalar::Util::weaken> whenever the attribute is set:
 
-  has 'parent' =>
-      ( is       => 'rw',
-        weak_ref => 1,
-      );
+  has 'parent' => (
+      is       => 'rw',
+      weak_ref => 1,
+  );
 
   $node->parent($parent_node);
 
@@ -385,10 +386,10 @@ circular references.
 A C<trigger> is a subroutine that is called whenever the attribute is
 set:
 
-  has 'size' =>
-      ( is      => 'rw',
-        trigger => \&_size_set,
-      );
+  has 'size' => (
+      is      => 'rw',
+      trigger => \&_size_set,
+  );
 
   sub _size_set {
       my ( $self, $size, $meta_attr ) = @_;
@@ -404,20 +405,20 @@ is called I<after> the value is set.
 
 Attributes can be restricted to only accept certain types:
 
-  has 'first_name' =>
-      ( is  => 'rw',
-        isa => 'Str',
-      );
+  has 'first_name' => (
+      is  => 'rw',
+      isa => 'Str',
+  );
 
 This says that the first_name attribute must be a string.
 
 Moose also provides a shortcut for specifying that an attribute only
 accepts objects that do a certain role:
 
-  has 'weapon' =>
-     ( is   => 'rw',
-       does => 'MyApp::Weapon',
-     );
+  has 'weapon' => (
+      is   => 'rw',
+      does => 'MyApp::Weapon',
+  );
 
 See the L<Moose::Manual::Types> documentation for a complete
 discussion of Moose's type system.
@@ -426,17 +427,17 @@ discussion of Moose's type system.
 
 Attributes can define methods which simple delegate to their values:
 
-  has 'hair_color' =>
-      ( is      => 'rw',
-        isa     => 'Graphics::Color::RGB',
-        handles => { hair_color_hex => 'as_hex_string' },
-      );
+  has 'hair_color' => (
+      is      => 'rw',
+      isa     => 'Graphics::Color::RGB',
+      handles => { hair_color_hex => 'as_hex_string' },
+  );
 
 This adds a new method, C<hair_color_hex>. When someone calls
 C<hair_color_hex>, internally, the object just calls C<<
 $self->hair_color->as_hex_string >>.
 
-See L<Moose::Manual::Delegation> for more details on how to set up
+See L<Moose::Manual::Delegation> for documentation on how to set up
 delegation methods.
 
 =head2 Metaclass and traits
@@ -449,11 +450,11 @@ traits for the attribute:
 
   use MooseX::AttributeHelpers;
 
-  has 'mapping' =>
-      ( metaclass => 'Collection::Hash',
-        is        => 'ro',
-        default   => sub { {} },
-      );
+  has 'mapping' => (
+      metaclass => 'Collection::Hash',
+      is        => 'ro',
+      default   => sub { {} },
+  );
 
 In this case, the metaclass C<Collection::Hash> really refers to
 C<MooseX::AttributeHelpers::Collection::Hash>.
@@ -462,13 +463,14 @@ You can also apply one or more traits to an attribute:
 
   use MooseX::MetaDescription;
 
-  has 'size' =>
-      ( is          => 'rw',
-        traits      => [ 'MooseX::MetaDescription::Meta::Trait' ],
-        description => { html_widget  => 'text_input',
-                         serialize_as => 'element',
-                       },
-      );
+  has 'size' => (
+      is          => 'rw',
+      traits      => ['MooseX::MetaDescription::Meta::Trait'],
+      description => {
+          html_widget  => 'text_input',
+          serialize_as => 'element',
+      },
+  );
 
 The advantage of traits is that you can mix more than one of them
 together easily (in fact, a trait is just a role under the hood).
@@ -478,7 +480,7 @@ attribute metaclasses and traits. See L<Moose::Manual::MooseX> for
 some examples. You can also write your own metaclasses and traits. See
 the "Meta" and "Extending" recipes in L<Moose::Cookbook> for examples.
 
-=head2 Attribute Inheritance
+=head1 ATTRIBUTE INHERITANCE
 
 By default, a child inherits all of its parent class(es)' attributes
 as-is. However, you can explicitly change some aspects of the
@@ -519,36 +521,42 @@ To override an attribute, you simply prepend its name with a plus sign
 
   extends 'Person';
 
-  has '+first_name' =>
-      ( lazy    => 1,
-        default => 'Bill',
-      );
+  has '+first_name' => (
+      lazy    => 1,
+      default => 'Bill',
+  );
 
 Now the C<first_name> attribute in C<LazyPerson> is lazy, and defaults
 to C<'Bill'>.
 
 We recommend that you exercise caution when changing the type (C<isa>)
-of an inherited attribute. It's best to only make the new type a
-subtype of the one accepted by the parent.
+of an inherited attribute.
+
+=head1 MORE ON ATTRIBUTES
+
+Moose attributes are a big topic, and this document glosses over a few
+aspects of their aspects. We recommend that you read the
+L<Moose::Manual::Delegation> and L<Moose::Manual::Types> documents to
+get a more complete understanding of attribute features.
+
+=head1 A FEW MORE OPTIONS
+
+Moose has lots of attribute options. The ones listed below are
+superceded by some more modern features, but are covered for the sake
+of completeness.
 
 =head2 The C<documentation> option
 
 You can provide a piece of documentation as a string for an attribute:
 
-  has 'first_name' =>
-      ( is            => 'rw',
-        documentation => q{The person's first (personal) name},
-      );
+  has 'first_name' => (
+      is            => 'rw',
+      documentation => q{The person's first (personal) name},
+  );
 
 Moose does absolutely nothing with this information other than store
 it.
 
-As an alternative, you might want to look at the
-C<MooseX::MetaDescription> module, which lets you attach a
-"description" to each attribute. This description is a hashref that
-can include meta-information intended for use in other code, as well
-as documentation information.
-
 =head2 The C<auto_deref> Option
 
 If your attribute is an array reference or hash reference, the
@@ -573,20 +581,13 @@ construction.
 This option is inherited from C<Class::MOP>, but we recommend that you
 use a C<builder> (which is Moose-only) instead.
 
-=head1 MORE ON ATTRIBUTES
-
-Moose attributes are a big topic, and this document glosses over a few
-topics. We recommend that you read the L<Moose::Manual::Delegation>
-and L<Moose::Manual::Types> documents to get a more complete
-understanding of attribute features.
-
 =head1 AUTHOR
 
 Dave Rolsky E<lt>autarch@urth.orgE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2008 by Infinity Interactive, Inc.
+Copyright 2009 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>