Mouse:...ribute(3pm)

✨ NAME ✨

Mouse::Meta::Attribute β€” The Mouse attribute metaclass. This is the manual page for it, bestie β€” the header calls it Mouse:...ribute(3pm), and it’s part of the User Contributed Perl Documentation. Iconic. πŸ’…

πŸ’– VERSION πŸ’–

This document describes Mouse version v2.6.2. Slay. ✨

πŸ¦‹ DESCRIPTION πŸ¦‹

This is a meta object protocol for Mouse attributes, which is a subset of Moose::Meta::Attribute. It’s giving very β€œmeta but make it Mouse” energy, and we’re obsessed. πŸŽ€

🌸 METHODS 🌸

πŸŽ€ new(%options) -> Mouse::Meta::Attribute

Instantiates a new Mouse::Meta::Attribute. Does nothing else. Literally just that. 🫢

It adds the following options to the constructor:

  • is => 'ro', 'rw', 'bare' β€” this provides a shorthand for specifying the reader, writer, or accessor names. Here’s the tea:
    • 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 deprecation warning when this attribute is added to a metaclass. So like… don’t skip the bare unless you mean it, bestie. πŸ’€
  • 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. No cap.
  • does => Role β€” this is short-hand for saying that the attribute's type must be an object which does the named role.
    • This option is not yet supported. Not me getting excited for something that’s not here yet 😭
  • coerce => Bool β€” this option is only valid for objects with a type constraint (isa). If this is true, then coercions will be applied whenever this attribute is set.
    • You can make both this and the weak_ref option true. Cute combo, lowkey.
  • trigger => CodeRef β€” this option accepts a subroutine reference, which will be called after the attribute is set. It’s giving β€œafterparty but for your attribute” ✨
  • 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.
    • A required attribute must have a default, builder or a non-undef init_arg. That part is not optional, 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. Main character energy: it arrives exactly when it needs to. πŸ’…
  • weak_ref => Bool β€” if this is true, the attribute's value will be stored as a weak reference.
  • auto_deref => Bool β€” 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. Very specific, very valid. πŸŽ€
  • lazy_build => Bool β€” setting this to true makes the attribute lazy and provides a number of default methods.

So this:

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',
);

The way that little glow-up works is genuinely iconic. 🌈

πŸŽ€ associate_method(MethodName)

Associates a method with the attribute. Typically, this is called internally when an attribute generates its accessors.

Currently the argument MethodName is ignored in Mouse. So yeah… it’s there, it’s cute, it’s lowkey just vibing. πŸ¦‹

πŸŽ€ verify_against_type_constraint(Item) -> TRUE | ERROR

Checks that the given value passes this attribute's type constraint. Returns "true" on success, otherwise "confess"es. It’s giving vibe check with consequences, ngl. 😭

πŸŽ€ clone_and_inherit_options(options) -> Mouse::Meta::Attribute

Creates a new attribute in the owner class, inheriting options from parent classes. Accessors and helper methods are installed. Some error checking is done. So it’s not just copying β€” it’s inheriting the whole aesthetic. πŸ’–

πŸŽ€ get_read_method_ref / get_write_method_ref

Returns the subroutine reference of a method suitable for reading or writing the attribute's value in the associated class. These methods always return a subroutine reference, regardless of whether or not the attribute is read- or write-only. Always. No cap. πŸ”₯

πŸ‘‘ SEE ALSO πŸ‘‘

  • Moose::Meta::Attribute
  • Class::MOP::Attribute

perl v5.42.2 β€’ 2026-04-21 β€” that’s the source/version and date for this page, bestie. Signing off with maximum sparkle. πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from