required, simply set the C<required> option to true:
has 'name' => (
- is => 'rw',
+ is => 'ro',
required => 1,
);
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',
);
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,
);
Attributes can be restricted to only accept certain types:
has 'first_name' => (
- is => 'rw',
+ is => 'ro',
isa => 'Str',
);
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' },
);
use MooseX::MetaDescription;
has 'size' => (
- is => 'rw',
+ is => 'ro',
traits => ['MooseX::MetaDescription::Meta::Trait'],
description => {
html_widget => 'text_input',