add note that has '+foo' does not work in roles that compose over another role
[gitmo/Moose.git] / lib / Moose / Manual / Attributes.pod
index 493a165..174c624 100644 (file)
@@ -2,7 +2,7 @@
 
 =head1 NAME
 
-Moose::Manual::Attribute - Object attributes with Moose
+Moose::Manual::Attributes - Object attributes with Moose
 
 =head1 INTRODUCTION
 
@@ -36,10 +36,10 @@ Use the C<has> function to declare an attribute:
 This says that all C<Person> objects have an optional read-write
 "first_name" attribute.
 
-=head2 Read-write Vs Read-only
+=head2 Read-write vs. 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
+attribute. There are 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).
 
@@ -47,7 +47,7 @@ need to set C<is>, which can be either C<rw> (read-write) or C<ro>
 that has no accessors, which is pointless unless you're doing some
 deep, dark magic).
 
-=head2 Accessor Methods
+=head2 Accessor methods
 
 Each attribute has one or more accessor methods. An accessor lets you
 read and write the value of that attribute for an object.
@@ -67,12 +67,12 @@ particularly handy when you'd like an attribute to be publicly
 readable, but only privately settable. For example:
 
   has 'weight' => (
-      is     => 'rw',
+      is     => 'ro',
       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
+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
 weight. This lets us hide the implementation details of weight
 changes, but still provide the weight value to users of the class.
 
@@ -91,10 +91,10 @@ C<writer> methods:
 
 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 override the default naming
+extension system that lets you override the default naming
 conventions. See L<Moose::Manual::MooseX> for more details.
 
-=head2 Predicate and Clearer Methods
+=head2 Predicate and clearer methods
 
 Moose allows you to explicitly distinguish between a false or
 undefined attribute value and an attribute which has not been set. If
@@ -102,8 +102,9 @@ 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 an attribute can be explicitly set to C<undef> or
-some other false value, but the predicate will return true.
+currently set. Note that an attribute can be explicitly set to
+C<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
@@ -145,23 +146,23 @@ predicate, and clearer method.
 By default, Moose does not make a predicate or clearer for you. You
 must explicitly provide names for them.
 
-=head2 Required or Not?
+=head2 Required or not?
 
 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',
+      is       => 'ro',
       required => 1,
   );
 
 There are a couple caveats worth mentioning in regards to what
 "required" actually means.
 
-Basically, all it says is that this attribute (C<name>) must be provided
-to the constructor. It does not say anything about its value, so it
-could be C<undef>.
+Basically, all it says is that this attribute (C<name>) must be provided to
+the constructor, or be lazy with either a default or a builder. It does not
+say anything about its value, so it could be C<undef>.
 
 If you define a clearer method on a required attribute, the clearer
 I<will> work, so even a required attribute can be unset after object
@@ -172,7 +173,7 @@ clearer doesn't make much sense. In some cases, it might be handy to
 have a I<private> C<clearer> and C<predicate> for a required
 attribute.
 
-=head2 Default and Builder Methods
+=head2 Default and builder methods
 
 Attributes can have default values, and Moose provides two ways to
 specify that default.
@@ -181,7 +182,7 @@ In the simplest form, you simply provide a non-reference scalar value
 for the C<default> option:
 
   has 'size' => (
-      is        => 'rw',
+      is        => 'ro',
       default   => 'medium',
       predicate => 'has_size',
   );
@@ -197,7 +198,7 @@ You can also provide a subroutine reference for C<default>. This
 reference will be called as a method on the object.
 
   has 'size' => (
-      is => 'rw',
+      is => 'ro',
       default =>
           sub { ( 'small', 'medium', 'large' )[ int( rand 3 ) ] },
       predicate => 'has_size',
@@ -206,10 +207,22 @@ reference will be called as a method on the object.
 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 attribute
-C<lazy>. Laziness is covered in the next section.
+When you provide a C<default> subroutine reference, it is called as a
+method on the object, with no additional parameters:
+
+  has 'size' => (
+      is => 'ro',
+      default => sub {
+          my $self = shift;
+
+          return $self->height > 200 ? 'big' : 'average';
+      },
+  );
+
+When the C<default> is 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
+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
@@ -217,7 +230,7 @@ Perl would instantiate the reference exactly once, and it would be
 shared by all objects:
 
   has 'mapping' => (
-      is      => 'rw',
+      is      => 'ro',
       default => {}, # wrong!
   );
 
@@ -229,7 +242,7 @@ end up shared across many objects. Instead, wrap it in a subroutine
 reference:
 
   has 'mapping' => (
-      is      => 'rw',
+      is      => 'ro',
       default => sub { {} }, # right!
   );
 
@@ -239,7 +252,7 @@ As an alternative to using a subroutine reference, you can instead
 supply a C<builder> method for your attribute:
 
   has 'size' => (
-      is        => 'rw',
+      is        => 'ro',
       builder   => '_build_size',
       predicate => 'has_size',
   );
@@ -250,19 +263,62 @@ supply a C<builder> method for your attribute:
 
 This has several advantages. First, it moves a chunk of code to its
 own named method, which improves readability and code
-organization. Second, the C<_build_size> method can be overridden in
-subclasses.
+organization.
 
 We strongly recommend that you use a C<builder> instead of a
 C<default> for anything beyond the most trivial default.
 
-=head2 Laziness and lazy_build
+A C<builder>, just like a C<default>, is called as a method on the
+object with no additional parameters.
+
+=head3 Builders allow subclassing
+
+Because the C<builder> is called I<by name>, it goes through Perl's
+method resolution. This means that builder methods are both
+inheritable and overridable.
+
+If we subclass our C<Person> class, we can override C<_build_size>:
+
+  package Lilliputian;
+
+  use Moose;
+  extends 'Person';
+
+  sub _build_size { return 'small' }
+
+=head3 Builders can be composed from roles
+
+Because builders are called by name, they work well with roles. For
+example, a role could provide an attribute but require that the
+consuming class provide the C<builder>:
+
+  package HasSize;
+  use Moose::Role;
+
+  requires '_build_size';
+
+  has 'size' => (
+      is      => 'ro',
+      lazy    => 1,
+      builder => '_build_size',
+  );
+
+  package Lilliputian;
+  use Moose;
+
+  with 'HasSize';
+
+  sub _build_size { return 'small' }
+
+Roles are covered in L<Moose::Manual::Roles>.
+
+=head2 Laziness and C<lazy_build>
 
 Moose lets you defer attribute population by making an attribute
 C<lazy>:
 
   has 'size' => (
-      is      => 'rw',
+      is      => 'ro',
       lazy    => 1,
       builder => '_build_size',
   );
@@ -289,32 +345,32 @@ 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',
+      is         => 'ro',
       lazy_build => 1,
   );
 
 This is the same as specifying all of these options:
 
   has 'size' => (
-      is        => 'rw',
+      is        => 'ro',
       lazy      => 1,
       builder   => '_build_size',
       clearer   => 'clear_size',
       predicate => 'has_size',
   );
 
-If your attribute name starts with an underscore (_), then the clearer
+If your attribute name starts with an underscore (C<_>), then the clearer
 and predicate will as well:
 
   has '_size' => (
-      is         => 'rw',
+      is         => 'ro',
       lazy_build => 1,
   );
 
 becomes:
 
   has '_size' => (
-      is        => 'rw',
+      is        => 'ro',
       lazy      => 1,
       builder   => '_build__size',
       clearer   => '_clear_size',
@@ -329,7 +385,7 @@ If you don't like the names that C<lazy_build> generates, you can
 always provide your own:
 
   has 'size' => (
-      is         => 'rw',
+      is         => 'ro',
       lazy_build => 1,
       clearer    => '_clear_size',
   );
@@ -337,7 +393,7 @@ always provide your own:
 Options that you explicitly provide are always used in favor of
 Moose's internal defaults.
 
-=head2 Constructor Parameters (init_arg)
+=head2 Constructor parameters (C<init_arg>)
 
 By default, each attribute can be passed by name to the class's
 constructor. On occasion, you may want to use a different name for
@@ -347,18 +403,18 @@ unsettable via the constructor.
 Both of these goals can be accomplished with the C<init_arg> option:
 
   has 'bigness' => (
-      is       => 'rw',
+      is       => 'ro',
       init_arg => 'size',
   );
 
-Now we have an attribute named bigness, but we pass C<size> to the
+Now we have an attribute named "bigness", but we pass C<size> to the
 constructor.
 
 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',
+      is         => 'ro',
       lazy_build => 1,
       init_arg   => undef,
   );
@@ -366,7 +422,7 @@ the constructor. This is particularly handy for private attributes:
 By setting the C<init_arg> to C<undef>, we make it impossible to set
 this attribute when creating a new object.
 
-=head2 Weak References
+=head2 Weak references
 
 Moose has built-in support for weak references. If you set the
 C<weak_ref> option to a true value, then it will call
@@ -393,25 +449,33 @@ set:
   );
 
   sub _size_set {
-      my ( $self, $size, $meta_attr ) = @_;
+      my ( $self, $size ) = @_;
 
       warn $self->name, " size is now $size\n";
   }
 
-The trigger is called as a method, and receives the new value as well
-as the L<Moose::Meta::Attribute> object for the attribute. The trigger
-is called I<after> the value is set.
+The trigger is called as a method, and receives the new value as its argument.
+The trigger is called I<after> the value is set.
+
+This differs from an after method modifier in two ways. First, a
+trigger is only called when the attribute is set, as opposed to
+whenever the accessor method is called (for reading or
+writing). Second, it is also called when an attribute's value is
+passed to the constructor.
 
-=head2 Attribute Types
+However, triggers are I<not> called when an attribute is populated
+from a C<default> or C<builder>
+
+=head2 Attribute types
 
 Attributes can be restricted to only accept certain types:
 
   has 'first_name' => (
-      is  => 'rw',
+      is  => 'ro',
       isa => 'Str',
   );
 
-This says that the first_name attribute must be a string.
+This says that the C<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:
@@ -426,10 +490,10 @@ discussion of Moose's type system.
 
 =head2 Delegation
 
-Attributes can define methods which simple delegate to their values:
+Attributes can define methods which simply delegate to their values:
 
   has 'hair_color' => (
-      is      => 'rw',
+      is      => 'ro',
       isa     => 'Graphics::Color::RGB',
       handles => { hair_color_hex => 'as_hex_string' },
   );
@@ -458,14 +522,14 @@ traits for the attribute:
   );
 
 In this case, the metaclass C<Collection::Hash> really refers to
-C<MooseX::AttributeHelpers::Collection::Hash>.
+L<MooseX::AttributeHelpers::Collection::Hash>.
 
 You can also apply one or more traits to an attribute:
 
   use MooseX::MetaDescription;
 
   has 'size' => (
-      is          => 'rw',
+      is          => 'ro',
       traits      => ['MooseX::MetaDescription::Meta::Trait'],
       description => {
           html_widget  => 'text_input',
@@ -514,7 +578,7 @@ The options that can be overridden in a subclass are:
 =back
 
 To override an attribute, you simply prepend its name with a plus sign
-(+):
+(C<+>):
 
   package LazyPerson;
 
@@ -536,9 +600,9 @@ 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.
+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
 
@@ -558,7 +622,7 @@ You can provide a piece of documentation as a string for an attribute:
 Moose does absolutely nothing with this information other than store
 it.
 
-=head2 The C<auto_deref> Option
+=head2 The C<auto_deref> option
 
 If your attribute is an array reference or hash reference, the
 C<auto_deref> option will make Moose dereference the value when it is
@@ -567,9 +631,9 @@ returned from the reader method:
   my %map = $object->mapping;
 
 This option only works if your attribute is explicitly typed as an
-ArrayRef or HashRef.
+C<ArrayRef> or C<HashRef>.
 
-However, we recommend that you use C<MooseX::AttributeHelpers> for
+However, we recommend that you use L<MooseX::AttributeHelpers> for
 these types of attributes, which gives you much more control over how
 they are accessed and manipulated.
 
@@ -579,7 +643,7 @@ Moose provides an attribute option called C<initializer>. This is
 similar to C<builder>, except that it is I<only> called during object
 construction.
 
-This option is inherited from C<Class::MOP>, but we recommend that you
+This option is inherited from L<Class::MOP>, but we recommend that you
 use a C<builder> (which is Moose-only) instead.
 
 =head1 AUTHOR