Moose::Meta::Attribute - The Moose attribute metaclass |
Moose::Meta::Attribute - The Moose attribute metaclass
This is a subclass of the Class::MOP::Attribute manpage with Moose specific extensions.
For the most part, the only time you will ever encounter an instance of this class is if you are doing some serious deep introspection. To really understand this class, you need to refer to the the Class::MOP::Attribute manpage documentation.
These methods override methods in the Class::MOP::Attribute manpage and add Moose specific features. You can safely assume though that they will behave just as the Class::MOP::Attribute manpage does.
eval { $point->meta->get_attribute('x')->set_value($point, 'fourty-two') }; if($@) { print "Oops: $@\n"; }
Attribute (x) does not pass the type constraint (Int) with 'fourty-two'
Before setting the value, a check is made on the type constraint of the attribute, if it has one, to see if the value passes it. If the value fails to pass, the set operation dies with a confess in the Carp manpage.
Any coercion to convert values is done before checking the type constraint.
To check a value against a type constraint before setting it, fetch the attribute instance using find_attribute_by_name in the Class::MOP::Class manpage, fetch the type_constraint from the attribute using type_constraint in the Moose::Meta::Attribute manpage and call check in the Moose::Meta::TypeConstraint manpage. See the Moose::Cookbook::RecipeX manpage for an example.
Moose attributes support type-constraint checking, weak reference creation and type coercion.
metaclass
and
traits
options.
has '+foo'
feature, it clones an attribute
from a superclass and allows a very specific set of changes to be made
to the attribute.
NOTE: lazy attributes, must have a default
or builder
field set.
lazy_build => 1
will
make your attribute required and lazy. In addition it will set the builder, clearer
and predicate options for you using the following convention.
#If your attribute name starts with an underscore: has '_foo' => (lazy_build => 1); #is the same as has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', builder => '_build__foo); # or has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', default => sub{shift->_build__foo});
#If your attribute name does not start with an underscore: has 'foo' => (lazy_build => 1); #is the same as has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', builder => '_build_foo); # or has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', default => sub{shift->_build_foo});
The reason for the different naming of the builder
is that the builder
method is a private method while the clearer
and predicate
methods
are public methods.
NOTE: This means your class should provide a method whose name matches the value of the builder part, in this case _build__foo or _build_foo.
NOTE: This can only be done for attributes whose type constraint is either ArrayRef or HashRef.
undef
.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <stevan@iinteractive.com>
Yuval Kogman <nothingmuch@woobling.com>
Copyright 2006-2008 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Moose::Meta::Attribute - The Moose attribute metaclass |