Moose::Meta::Attribute(3) | User Contributed Perl Documentation | Moose::Meta::Attribute(3) |
Moose::Meta::Attribute - The Moose attribute metaclass
version 2.2012
This class is a subclass of Class::MOP::Attribute that provides additional Moose-specific functionality.
To really understand this class, you will need to start with the Class::MOP::Attribute documentation. This class can be understood as a set of additional features on top of the basic feature provided by that parent class.
"Moose::Meta::Attribute" is a subclass of Class::MOP::Attribute.
Many of the documented below override methods in Class::MOP::Attribute and add Moose specific features.
Many of the options below are described in more detail in the Moose::Manual::Attributes document.
It adds the following options to the constructor:
This provides a shorthand for specifying the "reader", "writer", or "accessor" names. If the attribute is read-only ('ro') then it will have a "reader" method with the same attribute as the name.
If it is read-write ('rw') then it will have an "accessor" method with the same name. If you provide an explicit "writer" for a read-write attribute, then you will have a "reader" with the same name as the attribute, and a "writer" with the name you provided.
Use 'bare' when you are deliberately not installing any methods (accessor, reader, etc.) associated with this attribute; otherwise, Moose will issue a warning when this attribute is added to a metaclass.
This option accepts a type. The type can be a string, which should be a type name. If the type name is unknown, it is assumed to be a class name.
This option can also accept a Moose::Meta::TypeConstraint object.
If you also provide a "does" option, then your "isa" option must be a class name, and that class must do the role specified with "does".
This is short-hand for saying that the attribute's type must be an object which does the named role.
This option is only valid for objects with a type constraint ("isa") that defined a coercion. If this is true, then coercions will be applied whenever this attribute is set.
You cannot make both this and the "weak_ref" option true.
This option accepts a subroutine reference, which will be called after the attribute is set.
An attribute which is required must be provided to the constructor. An attribute which is required can also have a "default" or "builder", which will satisfy its required-ness.
A required attribute must have a "default", "builder" or a non-"undef" "init_arg"
A lazy attribute must have a "default" or "builder". When an attribute is lazy, the default value will not be calculated until the attribute is read.
If this is true, the attribute's value will be stored as a weak reference.
An arbitrary string that can be retrieved later by calling "$attr->documentation".
Note that in cases where you want this feature you are often better served by using a Moose::Meta::Attribute::Native trait instead.
If this is true, then the reader will dereference the value when it is called. The attribute must have a type constraint which defines the attribute as an array or hash reference.
Note that use of this feature is strongly discouraged. Some documentation used to encourage use of this feature as a best practice, but we have changed our minds.
Setting this to true makes the attribute lazy and provides a number of default methods.
has 'size' => ( is => 'ro', lazy_build => 1, );
is equivalent to this:
has 'size' => ( is => 'ro', 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 => 'ro', lazy_build => 1, );
becomes:
has '_size' => ( is => 'ro', 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 builder name.
If provided, this should be a Moose::Meta::Role::Attribute object.
The %options can only specify options handled by Class::MOP::Attribute.
This overrides the Class::MOP::Attribute method to handle lazy attributes, weak references, and type constraints.
eval { $point->meta->get_attribute('x')->set_value($point, 'forty-two') }; if($@) { print "Oops: $@\n"; }
Attribute (x) does not pass the type constraint (Int) with 'forty-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.
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 Class::MOP::Class, fetch the type_constraint from the attribute using "type_constraint" in Moose::Meta::Attribute and call "check" in Moose::Meta::TypeConstraint. See Moose::Cookbook::Basics::Company_Subtypes for an example.
If, after installing all methods, the attribute object has no associated methods, it throws an error unless "is => 'bare'" was passed to the attribute constructor. (Trying to add an attribute that has no associated methods is almost always an error.)
These methods are not found in the superclass. They support features provided by Moose.
Note that this checks the attribute itself, not its type constraint, so it is checking the attribute's metaclass and any traits applied to the attribute.
Effectively, this method is a factory that finds or creates the appropriate class for the given "metaclass" and/or "traits".
Once it has the appropriate class, it will call "$class->new($name, %options)" on that class.
One of its main tasks is to make sure that the %options provided does not include the options returned by the "illegal_options_for_inheritance" method.
This exists to allow a custom metaclass to change or add to the list of options which can not be changed.
See "BUGS" in Moose for details on reporting bugs.
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2019-11-22 | perl v5.34.0 |