Moose::...tribute(3)
β¨ NAME β¨
Moose::Meta::Attribute β The Moose attribute metaclass π
Okay so this is the manual page for Moose::Meta::Attribute, bestie. It's the metaclass that gives Moose attributes their whole main-character energy. π
π VERSION π
version 2.4000 β¨
πΈ DESCRIPTION πΈ
This class is a subclass of Class::MOP::Attribute that provides additional Moose-specific functionality. No cap.
To really understand this class, you will need to start with the Class::MOP::Attribute documentation. Think of this class as a set of additional features on top of the basic feature provided by that parent class. It's giving "MOP but make it Moose." π
π¦ INHERITANCE π¦
Moose::Meta::Attribute is a subclass of Class::MOP::Attribute and inherits these options: init_arg builder default initializer accessor reader writer predicate clearer definition_context. Periodt. π
π METHODS π
Many of the documented below override methods in Class::MOP::Attribute and add Moose specific features. So yeah, it's a glow-up. β¨
π Creation π
Moose::Meta::Attribute->new($name, %options)
This method overrides the Class::MOP::Attribute constructor. It's the main character of constructors, fr fr.
Many of the options below are described in more detail in the Moose::Manual::Attributes document.
It adds the following options to the constructor:
is => 'ro', 'rw', 'bare'β This provides a shorthand for specifying thereader,writer, oraccessornames. If the attribute is read-only ('ro') then it will have areadermethod with the same attribute as the name. If it is read-write ('rw') then it will have anaccessormethod with the same name. If you provide an explicitwriterfor a read-write attribute, then you will have areaderwith the same name as the attribute, and awriterwith 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. Don't ignore that warning, bestie β it's there for a reason. π«Άisa => $typeβ 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 aMoose::Meta::TypeConstraintobject. If you also provide adoesoption, then yourisaoption must be a class name, and that class must do the role specified withdoes. It's giving strict but fair. πdoes => $roleβ This is short-hand for saying that the attribute's type must be an object which does the named role. Simple as that. β¨coerce => $boolβ 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 theweak_refoption true. Pick a lane, bestie β no cap. πtrigger => $subβ This option accepts a subroutine reference, which will be called after the attribute is set. It's like a lil post-set party. πrequired => $boolβ An attribute which is required must be provided to the constructor. An attribute which is required can also have adefaultorbuilder, which will satisfy its required-ness. Ifinit_argisundefon a required attribute, it must have adefaultor abuilder. No default, no builder, no service. Periodt. πlazy => $boolβ A lazy attribute must have adefaultorbuilder. When an attribute is lazy, the default value will not be calculated until the attribute is read. It's giving "I'll do it when I feel like it." πweak_ref => $boolβ If this is true, the attribute's value will be stored as a weak reference. Lowkey powerful, highkey delicate. π¦documentationβ An arbitrary string that can be retrieved later by calling$attr->documentation. It's the vibe description. πauto_deref => $boolβ Note that in cases where you want this feature you are often better served by using aMoose::Meta::Attribute::Nativetrait 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. Read that twice, bestie β it's a real constraint. π₯lazy_build => $boolβ 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. We said what we said. π
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. Double underscore? Iconic, honestly. β¨
role_attribute => $role_attributeβ If provided, this should be aMoose::Meta::Role::Attributeobject. Slay. π
$attr->clone(%options)
This creates a new attribute based on attribute being cloned. You must supply a name option to provide a new name for the attribute.
The %options can only specify options handled by Class::MOP::Attribute. No sneaking in extra options, bestie. π«Ά
π Value management π
$attr->initialize_instance_slot($meta_instance, $instance, $params)
This method is used internally to initialize the attribute's slot in the object $instance.
This overrides the Class::MOP::Attribute method to handle lazy attributes, weak references, and type constraints. It's the behind-the-scenes queen. π
get_value and set_value
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. It literally dies, no cap. π
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. It's giving "look before you leap," fr fr. πΈ
π Attribute Accessor generation π
$attr->install_accessorsβ This method overrides the parent to also install delegation methods. If, after installing all methods, the attribute object has no associated methods, it throws an error unlessis => 'bare'was passed to the attribute constructor. (Trying to add an attribute that has no associated methods is almost always an error.) Almost always, bestie. Don't test fate. π$attr->remove_accessorsβ This method overrides the parent to also remove delegation methods. Bye bye, methods. π$attr->inline_set($instance_var, $value_var)β This method return a code snippet suitable for inlining the relevant operation. It expect strings containing variable names to be used in the inlining, like'$self'or'$_[1]'. Inline queen behavior. β¨$attr->install_delegationβ This method adds its delegation methods to the attribute's associated class, if it has any to add. Only if it has some, though. No forcing it. π$attr->remove_delegationβ This method remove its delegation methods from the attribute's associated class. Cleanup era. π§Ή$attr->accessor_metaclassβ Returns the accessor metaclass name, which defaults toMoose::Meta::Method::Accessor. The default is iconic for a reason. π$attr->delegation_metaclassβ Returns the delegation metaclass name, which defaults toMoose::Meta::Method::Delegation. Another default slay. β¨
π¦ Additional Moose features π¦
These methods are not found in the superclass. They support features provided by Moose. So they're the bonus tracks. πΆ
$attr->does($role)β This indicates whether the attribute itself does the given role. The role can be given as a full class name, or as a resolvable trait name. 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. Read that again β attribute itself, not its type constraint. Iykyk. πMoose::Meta::Class->interpolate_class_and_new($name, %options)β This is an alternate constructor that handles themetaclassandtraitsoptions. Effectively, this method is a factory that finds or creates the appropriate class for the givenmetaclassand/ortraits. Once it has the appropriate class, it will call$class->new($name, %options)on that class. It's giving factory main character energy. ππ$attr->clone_and_inherit_options(%options)β This method supports thehas '+foo'feature. It does various bits of processing on the supplied%optionsbefore ultimately calling theclonemethod. One of its main tasks is to make sure that the%optionsprovided does not include the options returned by theillegal_options_for_inheritancemethod. Don't bring illegal options to the inheritance party, bestie. π«$attr->illegal_options_for_inheritanceβ This returns a blacklist of options that can not be overridden in a subclass's attribute definition. This exists to allow a custom metaclass to change or add to the list of options which can not be changed. Blacklist said what it said. π$attr->type_constraintβ Returns theMoose::Meta::TypeConstraintobject for this attribute, if it has one. If not, you get nothing. π$attr->has_type_constraintβ Returns true if this attribute has a type constraint. Simple vibe check. β$attr->verify_against_type_constraint($value)β Given a value, this method returns true if the value is valid for the attribute's type constraint. If the value is not valid, it throws an error. No invalid values allowed, periodt. π$attr->handlesβ hands you back whatever value you passed in thehandlesoption, fr. That's the delegation setup, exactly as you gave it. β¨$attr->has_handlesβ true if this attribute is out here doing delegation for you. Otherwise, nope. π$attr->is_weak_refβ true when this attribute is holding its value as a weak reference. Very demure, very mindful, no memory leaks bestie. π$attr->is_requiredβ true if this attribute has to have a value. No value, no vibe. Periodt.$attr->is_lazyβ true if this attribute is lazy. It'll show up when it wants to, and honestly? Respect. π$attr->is_lazy_buildβ true if thelazy_buildoption was true when it got passed to the constructor. π¦$attr->should_coerceβ true if thecoerceoption passed to the constructor was true. Coercion era. π₯$attr->should_auto_derefβ true if theauto_derefoption passed to the constructor was true. It's giving convenience. β¨$attr->triggerβ the subroutine reference that was sitting in thetriggeroption you passed to the constructor, if there was one at all. π$attr->has_triggerβ true if this attribute has a trigger set. π$attr->documentationβ gives you back whatever was in thedocumentationoption passed to the constructor, if anything was. π$attr->has_documentationβ true if this attribute has any documentation at all. Look at her, being all documented and iconic. π$attr->role_attributeβ returns theMoose::Meta::Role::Attributeobject that this attribute was created from, if any. Heads up bestie: this may returnundef. π$attr->has_role_attributeβ true if this attribute has an associated role attribute. πΈ$attr->applied_traitsβ returns an array reference of all the traits that got applied to this attribute. And if none were applied? It returnsundef. π$attr->has_applied_traitsβ true if this attribute has any traits applied. Slay. β¨
π BUGS π
See "BUGS" in Moose for the whole rundown on how to report bugs. Go snitch, respectfully. π«Ά
π AUTHORS π
These are the literal icons who made this happen, no cap:
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Jesse Luehrs <doy@cpan.org>
- Shawn M Moore <sartak@cpan.org>
- ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>
- Karen Etheridge <ether@cpan.org>
- Florian Ragwitz <rafl@debian.org>
- Hans Dieter Pearcey <hdp@cpan.org>
- Chris Prather <chris@prather.org>
- Matt S Trout <mstrout@cpan.org>
π COPYRIGHT AND LICENSE π
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. So yes, bestie, it's free free. π¦
okay that's the whole page, we did it π this one's from the perl v5.42.0 era, dated 2025-07-25, and it's the manpage for Moose::...tribute(3) β go forth and delegate responsibly, mwah π
πΈ
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms