# perldoc > Class::MOP::Attribute

---
type: CommandReference
command: Class::MOP::Attribute
mode: perldoc
section: ''
source: perldoc
---

## Quick Reference

- `Class::MOP::Attribute->new($name, %options)` — create attribute meta-object
- `$attr->name` — get attribute name
- `$attr->default($instance)` — get default value, calling coderef as method if provided
- `$attr->set_value($instance, $value)` — set attribute value directly (bypasses accessor)
- `$attr->get_value($instance)` — get attribute value directly (bypasses accessor)
- `$attr->has_value($instance)` — check if attribute is set in instance
- `$attr->install_accessors` — generate and install accessor methods in the associated class
- `$attr->associated_class` — returns the `Class::MOP::Class` with which this attribute is associated

## Name

Attribute Meta Object — a consistent API for creating and managing object attributes in the MOP.

## Synopsis

perl
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 looks for a -foo key
    default   => 'BAR IS BAZ!'    # default if no -foo key provided
));

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 looks for a :bar key
                                  # no default means it is undef
));
## Options (constructor parameters)

These are passed as key-value pairs after the attribute name.

- `init_arg` — string key expected in the initialization hash; defaults to attribute name. Set to `undef` to prevent initialization.
- `builder` — method name (string) called on the object after construction to initialize the attribute.
- `default` — explicit default value. If a coderef, it is called as a method on the object. Wrap HASH/ARRAY refs in a coderef. For a coderef default, wrap in another coderef.
- `initializer` — method name or coderef called when a value is provided to the constructor. Receives `($self, $value, $set_coderef, $attr)`. The `$set_coderef` is called to actually set the value.
- `accessor` — name of a read/write accessor method, or a hashref `{ method_name => sub { ... } }` to install a custom subroutine.
- `reader` — name of read-only accessor method, or hashref with custom sub.
- `writer` — name of write-only accessor method, or hashref with custom sub.
- `predicate` — name of predicate method (returns boolean if attribute has been explicitly set), or hashref with custom sub.
- `clearer` — name of method to clear the attribute (makes predicate return false), or hashref with custom sub.
- `definition_context` — hashref containing `line` and either `file` or `description`; used for error reporting during inlining (primarily for Moose).

## Methods

### Creation and cloning

- `$attr->clone(%options)` — clones the attribute, overriding options with those provided. Change the name by passing a `name` key.

### Informational

- `$attr->name` — returns attribute name.
- `$attr->accessor`, `$attr->reader`, `$attr->writer`, `$attr->predicate`, `$attr->clearer` — return the value passed to the constructor (string or hashref).
- `$attr->initializer` — returns the initializer value (method name or coderef).
- `$attr->init_arg` — returns the init_arg string.
- `$attr->is_default_a_coderef` — boolean.
- `$attr->builder` — returns the builder method name.
- `$attr->default($instance)` — without argument, returns the raw value. With an instance, if the default is a coderef, it is called as a method on the instance and the result is returned.
- `$attr->slots` — returns a list of slot names (usually just the attribute name).
- `$attr->get_read_method` — returns the name of the read method, or `undef` if write-only.
- `$attr->get_write_method` — returns the name of the write method, or `undef` if read-only.
- `$attr->has_read_method` — boolean indicating existence of a named read method.
- `$attr->has_write_method` — boolean indicating existence of a named write method.
- `$attr->get_read_method_ref` — returns a subroutine reference for reading (always returns a ref).
- `$attr->get_write_method_ref` — returns a subroutine reference for writing (always returns a ref).
- `$attr->insertion_order` — if the attribute has been inserted into a class, returns the zero-based insertion index.

### Informational predicates

- `$attr->has_accessor`, `$attr->has_reader`, `$attr->has_writer`, `$attr->has_predicate`, `$attr->has_clearer`, `$attr->has_initializer` — boolean.
- `$attr->has_init_arg` — false if `init_arg` was explicitly set to `undef`.
- `$attr->has_default` — false if `default` was set to `undef` (the default default).
- `$attr->has_builder` — boolean.
- `$attr->has_insertion_order` — false if the attribute has not been inserted into a class.

### Value management (back doors to the instance)

- `$attr->initialize_instance_slot($meta_instance, $instance, $params)` — called internally to initialize the attribute slot. Not for general use.
- `$attr->set_value($instance, $value)` — sets the value directly, bypassing the accessor. Works even with read-only attributes.
- `$attr->set_raw_value($instance, $value)` — sets the value with no side effects (e.g., no trigger). (Does not apply to `Class::MOP` itself, only subclasses.)
- `$attr->set_initial_value($instance, $value)` — sets the value during first initialization, bypassing the accessor.
- `$attr->get_value($instance)` — returns the value directly, bypassing the accessor. Works even with write-only attributes.
- `$attr->get_raw_value($instance)` — returns the value with no side effects (e.g., lazy attributes). (Does not apply to `Class::MOP` itself.)
- `$attr->has_value($instance)` — boolean indicating whether the attribute has been set in the instance.
- `$attr->clear_value($instance)` — clears the attribute value in the instance. Works even if the attribute has no associated methods.

### Class association

- `$attr->associated_class` — returns the `Class::MOP::Class` with which this attribute is associated, or `undef`.
- `$attr->attach_to_class($metaclass)` — stores a weakened reference to the metaclass. Does not remove from old class or create accessors. Prefer `Class::MOP::Class`'s `add_attribute`.
- `$attr->detach_from_class` — removes the associated metaclass. Does not remove attribute from class or remove accessors. Prefer `Class::MOP::Class`'s `remove_attribute`.

### Attribute accessor generation

- `$attr->accessor_metaclass` — returns the name of the accessor metaclass (default: `Class::MOP::Method::Accessor`).
- `$attr->associate_method($method)` — associates a `Class::MOP::Method` object with the attribute. Called internally.
- `$attr->associated_methods` — returns the list of methods associated with the attribute.
- `$attr->install_accessors` — generates and installs accessor methods. Typically called by `Class::MOP::Class`'s `add_attribute`.
- `$attr->remove_accessors` — removes all associated accessor methods. Does not modify the `associated_methods` list.
- `$attr->inline_get`, `$attr->inline_set`, `$attr->inline_has`, `$attr->inline_clear` — return code snippets suitable for inlining. Expect variable names like `'$self'` or `'$_[1]'`.

### Introspection

- `Class::MOP::Attribute->meta` — returns a `Class::MOP::Class` instance for this class.

## See Also

- [Class::MOP](https://metacpan.org/pod/Class::MOP)
- [Class::MOP::Class](https://metacpan.org/pod/Class::MOP::Class)
- [Class::MOP::Method::Accessor](https://metacpan.org/pod/Class::MOP::Method::Accessor)
- [Moose](https://metacpan.org/pod/Moose)