Class::MOP::Attribute - Attribute Meta Object
| Use Case | Command | Description |
|---|---|---|
| ๐๏ธ Create a new attribute | Class::MOP::Attribute->new( 'name' => ( ... ) ) | Define an attribute with options like accessor, default, init_arg, etc. |
| ๐ฆ Set default value | default => 'scalar' or default => sub { [] } | Provide a scalar or a subroutine reference for default initialization. |
| ๐ง Use builder method | builder => 'build_name' | Call a method on the object to initialize the attribute. |
| ๐ Custom initializer | initializer => sub { ... } or initializer => 'method_name' | Munge the constructor value before setting. |
| ๐ Check attribute set | predicate => 'has_name' | Returns true if attribute has been explicitly set (even to false). |
| ๐งน Clear attribute | clearer => 'clear_name' | Uninitialize the attribute, predicate returns false. |
| ๐ Read-only accessor | reader => 'name' | Get value without setting. |
| โ๏ธ Write-only accessor | writer => 'set_name' | Set value without getting. |
| ๐ Read/write accessor | accessor => 'name' | Standard Perl-style get/set. |
| ๐ Access value directly (MOP) | $attr->get_value($instance) / set_value($instance, $value) | Bypass accessors, even for read-only/write-only attributes. |
version 2.2200
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
)
);
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.
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.
MyClass->meta->new_object( -foo => 'Hello There' );
If an init_arg is not assigned, it will automatically use the attribute's name. If "init_arg" is explicitly set to "undef", the attribute cannot be specified during initialization.
If the value is a simple scalar (string or number), then it can be just passed as is. However, if you wish to initialize it with a HASH or ARRAY ref, then you need to wrap that inside a subroutine reference:
Class::MOP::Attribute->new(
'foo' => (
default => sub { [] },
)
);
# or ...
Class::MOP::Attribute->new(
'foo' => (
default => sub { {} },
)
);
If you wish to initialize an attribute with a subroutine reference itself, then you need to wrap that in a subroutine as well:
Class::MOP::Attribute->new(
'foo' => (
default => sub {
sub { print "Hello World" }
},
)
);
And lastly, if the value of your attribute is dependent upon some other aspect of the instance structure, then you can take advantage of the fact that when the "default" value is called as a method:
Class::MOP::Attribute->new(
'object_identity' => (
default => sub { Scalar::Util::refaddr( $_[0] ) },
)
);
Note that there is no guarantee that attributes are initialized in any particular order, so you cannot rely on the value of some other attribute when generating the default.
The initializer is called as a method with three arguments. The first is the value that was passed to the constructor. The second is a subroutine reference that can be called to actually set the attribute's value, and the last is the associated "Class::MOP::Attribute" object.
This contrived example shows an initializer that sets the attribute to twice the given value.
Class::MOP::Attribute->new(
'doubled' => (
initializer => sub {
my ( $self, $value, $set, $attr ) = @_;
$set->( $value * 2 );
},
)
);
Since an initializer can be a method name, you can easily make attribute initialization use the writer:
Class::MOP::Attribute->new(
'some_attr' => (
writer => 'some_attr',
initializer => 'some_attr',
)
);
Your writer (actually, a wrapper around the writer, using method modifications) will need to examine @_ and determine under which context it is being called:
around 'some_attr' => sub { my $orig = shift; my $self = shift; # $value is not defined if being called as a reader # $setter and $attr are only defined if being called as an initializer my ($value, $setter, $attr) = @_; # the reader behaves normally return $self->$orig if not @_; # mutate $value as desired # $value = accessor,$attr->reader,$attr->writer,$attr->predicate,$attr->clearerโ All return exactly what was passed to the constructor, so it can be either a string containing a method name, or a hash reference.
$attr->initializer โ Returns the initializer as passed to the constructor, so this may be either a method name or a subroutine reference.$attr->init_arg, $attr->is_default_a_coderef, $attr->builder, $attr->default($instance) โ The $instance argument is optional. If you don't pass it, the return value for this method is exactly what was passed to the constructor, either a simple scalar or a subroutine reference. If you do pass an $instance and the default is a subroutine reference, then the reference is called as a method on the $instance and the generated value is returned.$attr->slots โ Return a list of slots required by the attribute. This is usually just one, the name of the attribute. A slot is the name of the hash key used to store the attribute in an object instance.$attr->get_read_method, $attr->get_write_method โ Returns the name of a method suitable for reading or writing the value of the attribute in the associated class. If an attribute is read- or write-only, then these methods can return "undef" as appropriate.$attr->has_read_method, $attr->has_write_method โ Returns a boolean indicating whether the attribute has a named read or write method.$attr->get_read_method_ref, $attr->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.$attr->insertion_order โ If this attribute has been inserted into a class, this returns a zero based index regarding the order of insertion.These are all basic predicate methods for the values passed into "new".
$attr->has_accessor, $attr->has_reader, $attr->has_writer, $attr->has_predicate, $attr->has_clearer, $attr->has_initializer$attr->has_init_arg โ This will be false if the "init_arg" was set to "undef".$attr->has_default โ This will be false if the "default" was set to "undef", since "undef" is the default "default" anyway.$attr->has_builder$attr->has_insertion_order โ This will be false if this attribute has not be inserted into a class.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.
$attr->initialize_instance_slot($meta_instance, $instance, $params) โ This method is used internally to initialize the attribute's slot in the object $instance. The $params is a hash reference of the values passed to the object constructor. It's unlikely that you'll need to call this method yourself.$attr->set_value($instance, $value) โ Sets the value without going through the accessor. Note that this works even with read-only attributes.$attr->set_raw_value($instance, $value) โ Sets the value with no side effects such as a trigger. This doesn't actually apply to Class::MOP attributes, only to subclasses.$attr->set_initial_value($instance, $value) โ Sets the value without going through the accessor. This method is only called when the instance is first being initialized.$attr->get_value($instance) โ Returns the value without going through the accessor. Note that this works even with write-only accessors.$attr->get_raw_value($instance) โ Returns the value without any side effects such as lazy attributes. Doesn't actually apply to Class::MOP attributes, only to subclasses.$attr->has_value($instance) โ Return a boolean indicating whether the attribute has been set in $instance. This how the default "predicate" method works.$attr->clear_value($instance) โ This will clear the attribute's value in $instance. This is what the default "clearer" calls. Note that this works even if the attribute does not have any associated read, write or clear methods.These methods allow you to manage the attribute's 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.
$attr->associated_class โ This returns the Class::MOP::Class with which this attribute is associated, if any.$attr->attach_to_class($metaclass) โ This method stores a weakened reference to the $metaclass object internally. This method does not remove the attribute from its old class, nor does it create any accessors in the new class. It is probably best to use the Class::MOP::Class "add_attribute" method instead.$attr->detach_from_class โ This method removes the associate metaclass object from the attribute it has one. This method does not remove the attribute itself from the class, or remove its accessors. It is probably best to use the Class::MOP::Class "remove_attribute" method instead.$attr->accessor_metaclass โ Accessor methods are generated using an accessor metaclass. By default, this is Class::MOP::Method::Accessor. This method returns the name of the accessor metaclass that this attribute uses.$attr->associate_method($method) โ This associates a Class::MOP::Method object with the attribute. Typically, this is called internally when an attribute generates its accessors.$attr->associated_methods โ This returns the list of methods which have been associated with the attribute.$attr->install_accessors โ This method generates and installs code for the attribute's accessors. It is typically called from the Class::MOP::Class "add_attribute" method.$attr->remove_accessors โ This method removes all of the accessors associated with the attribute. This does not currently remove methods from the list returned by "associated_methods".$attr->inline_get, $attr->inline_set, $attr->inline_has, $attr->inline_clear โ These methods return a code snippet suitable for inlining the relevant operation. They expect strings containing variable names to be used in the inlining, like '$self' or '$_[1]'.Class::MOP::Attribute->meta โ This will return a Class::MOP::Class instance for this class. It should also be noted that Class::MOP will actually bootstrap this module by installing a number of attribute meta-objects into its metaclass.
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.
Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-20 18:31 @2600:1f28:365:80b0:8802:8bb4:3873:328e
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format