info > Class::MOP::Attribute

📛 NAME

Class::MOP::Attribute - Attribute Meta Object

🚀 Quick Reference

Use Case Command Description
Create a new attribute Class::MOP::Attribute->new($name, %options) Define a new attribute meta-object
Read/Write accessor accessor => 'name' Generates a dual-purpose get/set accessor
Read-only accessor reader => 'name' Generates a getter only
Write accessor writer => 'name' Generates a setter only
Predicate check predicate => 'has_name' Check if attribute is initialized
Clear attribute clearer => 'clear_name' Uninitialize the attribute
Default value default => 'value' Provide a default scalar value
Default reference default => sub { [] } Provide a default reference (must be in sub)
Builder method builder => 'method_name' Method called to initialize attribute
Initializer initializer => sub { ... } Munge constructor value
Init argument init_arg => '-foo' Custom constructor key
Clone attribute $attr->clone(%options) Clone with overridden options

📌 VERSION

version 2.2200

📝 SYNOPSIS

Class::MOP::Attribute->new(
    foo => (
        accessor  => 'foo',           # dual purpose get/set accessor
        predicate => 'has_foo',       # predicate check for defined-ness
        init_arg  => '-foo',          # class->new will look for a -foo key
        default   => 'BAR IS BAZ!'    # if no -foo key is provided, use this
    )
);

Class::MOP::Attribute->new(
    bar => (
        reader    => 'bar',           # getter
        writer    => 'set_bar',       # setter
        predicate => 'has_bar',       # predicate check for defined-ness
        init_arg  => ':bar',          # class->new will look for a :bar key
                                      # no default value means it is undef
    )
);

📖 DESCRIPTION

The Attribute Protocol is almost entirely an invention of Class::MOP. Perl 5 does not have a consistent notion of attributes. There are so many ways in which this is done, and very few (if any) are easily discoverable by this module.

With that said, this module attempts to inject some order into this chaos, by introducing a consistent API which can be used to create object attributes.

âš™ī¸ METHODS

đŸ› ī¸ Creation

Class::MOP::Attribute->new($name, ?%options)

An attribute must (at the very least), have a $name. All other %options are added as key-value pairs.

The "accessor", "reader", "writer", "predicate" and "clearer" options all accept the same parameters. You can provide the name of the method, in which case an appropriate default method will be generated for you. Or instead you can also provide hash reference containing exactly one key (the method name) and one value. The value should be a subroutine reference, which will be installed as the method itself.

$attr->clone(%options)

This clones the attribute. Any options you provide will override the settings of the original attribute. You can change the name of the new attribute by passing a "name" key in %options.

â„šī¸ Informational

These are all basic read-only accessors for the values passed into the constructor.

🔍 Informational predicates

These are all basic predicate methods for the values passed into "new".

💾 Value management

These methods are basically "back doors" to the instance, and can be used to bypass the regular accessors, but still stay within the MOP. These methods are not for general use, and should only be used if you really know what you are doing.

🔗 Class association

These methods allow you to manage the attributes association with the class that contains it. These methods should not be used lightly, nor are they very magical, they are mostly used internally and by metaclass instances.

⚡ Attribute Accessor generation

🔎 Introspection

đŸ‘Ĩ AUTHORS

ÂŠī¸ 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.

Class::MOP::Attribute
📛 NAME 🚀 Quick Reference 📌 VERSION 📝 SYNOPSIS 📖 DESCRIPTION âš™ī¸ METHODS
đŸ› ī¸ Creation â„šī¸ Informational 🔍 Informational predicates 💾 Value management 🔗 Class association ⚡ Attribute Accessor generation 🔎 Introspection
đŸ‘Ĩ AUTHORS ÂŠī¸ COPYRIGHT AND LICENSE

Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-16 09:14 @2600:1f28:365:80b0:7cb9:fb:26c1:e368
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!