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 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. 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 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. 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 the weak_ref option 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 a default or builder, which will satisfy its required-ness. If init_arg is undef on a required attribute, it must have a default or a builder. No default, no builder, no service. Periodt. πŸ‘‘
  • lazy => $bool β€” 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. 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 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. 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 a Moose::Meta::Role::Attribute object. 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 unless is => '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 to Moose::Meta::Method::Accessor. The default is iconic for a reason. πŸ‘‘
  • $attr->delegation_metaclass β€” Returns the delegation metaclass name, which defaults to Moose::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 the metaclass and traits options. 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. It's giving factory main character energy. πŸ­πŸ‘‘
  • $attr->clone_and_inherit_options(%options) β€” This method supports the has '+foo' feature. It does various bits of processing on the supplied %options before ultimately calling the clone method. 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. 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 the Moose::Meta::TypeConstraint object 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 the handles option, 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 the lazy_build option was true when it got passed to the constructor. πŸ¦‹
  • $attr->should_coerce β€” true if the coerce option passed to the constructor was true. Coercion era. πŸ”₯
  • $attr->should_auto_deref β€” true if the auto_deref option passed to the constructor was true. It's giving convenience. ✨
  • $attr->trigger β€” the subroutine reference that was sitting in the trigger option 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 the documentation option 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 the Moose::Meta::Role::Attribute object that this attribute was created from, if any. Heads up bestie: this may return undef. πŸ™ˆ
  • $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 returns undef. πŸ’€
  • $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>

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