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).
-
-(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).
+(read-only). When an attribute is C<rw>, you can change it by passing
+a value to its accessor. When an attribute is C<ro>, you may only read
+the current value of the attribute.
+
+In fact, you could even omit C<is>, but that gives you an attribute
+that has no accessor. This can be useful with other attribute options,
+such as C<handles>. However, if your attribute generates I<no>
+accessors, Moose will issue a warning, because that usually means the
+programmer forgot to say the attribute is read-only or read-write. If
+you really mean to have no accessors, you can silence this warning by
+setting C<is> to C<bare>.
=head2 Accessor methods
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.
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
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
my $person2 = Person->new( ssn => '111-22-3333');
$person2->has_ssn; # true
-By default, Moose does not make a predicate or clearer for you. You
-must explicitly provide names for them.
+By default, Moose does not make a predicate or clearer for you. You must
+explicitly provide names for them, and then Moose will create the methods
+for you.
=head2 Required or not?
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
for the C<default> option:
has 'size' => (
- is => 'rw',
+ is => 'ro',
default => 'medium',
predicate => 'has_size',
);
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',
method on the object, with no additional parameters:
has 'size' => (
- is => 'rw',
+ is => 'ro',
default => sub {
my $self = shift;
shared by all objects:
has 'mapping' => (
- is => 'rw',
+ is => 'ro',
default => {}, # wrong!
);
reference:
has 'mapping' => (
- is => 'rw',
+ is => 'ro',
default => sub { {} }, # right!
);
supply a C<builder> method for your attribute:
has 'size' => (
- is => 'rw',
+ is => 'ro',
builder => '_build_size',
predicate => 'has_size',
);
has 'size' => (
is => 'ro',
lazy => 1,
- builder => '_build_animal',
+ builder => '_build_size',
);
package Lilliputian;
C<lazy>:
has 'size' => (
- is => 'rw',
+ is => 'ro',
lazy => 1,
builder => '_build_size',
);
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',
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',
always provide your own:
has 'size' => (
- is => 'rw',
+ is => 'ro',
lazy_build => 1,
clearer => '_clear_size',
);
Both of these goals can be accomplished with the C<init_arg> option:
has 'bigness' => (
- is => 'rw',
+ is => 'ro',
init_arg => 'size',
);
the constructor. This is particularly handy for private attributes:
has '_genetic_code' => (
- is => 'rw',
+ is => 'ro',
lazy_build => 1,
init_arg => undef,
);
);
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
+This differs from an C<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 if the when an attribute's value
-is passed to the constructor.
+writing). Second, it is also called when an attribute's value is
+passed to the constructor.
However, triggers are I<not> called when an attribute is populated
from a C<default> or C<builder>
Attributes can be restricted to only accept certain types:
has 'first_name' => (
- is => 'rw',
+ is => 'ro',
isa => 'Str',
);
=head2 Delegation
-Attributes can define methods which simply delegate to their values:
+An attribute can define methods which simply delegate to its value:
has 'hair_color' => (
- is => 'rw',
+ is => 'ro',
isa => 'Graphics::Color::RGB',
handles => { hair_color_hex => 'as_hex_string' },
);
use MooseX::MetaDescription;
has 'size' => (
- is => 'rw',
+ is => 'ro',
traits => ['MooseX::MetaDescription::Meta::Trait'],
description => {
html_widget => 'text_input',
=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