Class::MOP::Attribute - Attribute Meta Object 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 this module, and is completely optional to this MOP. This is because Perl 5 does not have consistent notion of what is an attribute of a class. There are so many ways in which this is done, and very few (if any) are easily discoverable by this module. So, all 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.

Creation
An attribute must (at the very least), have a $name. All other %options are contained added as key-value pairs. Acceptable keys are as follows: This should be a string value representing the expected key in an initialization hash. For instance, if we have an init_arg value of "-foo", then the following code will Just Work. MyClass->meta->construct_instance(-foo => "Hello There"); In an init_arg is not assigned, it will automatically use the value of $name. If an explicit "undef" is given for an init_arg, an attribute value can't be specified during initialization. The value of this key is the name of the method that will be called to obtain the value used to initialize the attribute. This should be a method in the class associated with the attribute, not a method in the attribute class itself. The value of this key is the default value which "Class::MOP::Class::construct_instance" will initialize the attribute to. NOTE: 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 CODE reference, like so: Class::MOP::Attribute->new('@foo' => ( default => sub { [] }, )); # or ... Class::MOP::Attribute->new('%foo' => ( default => sub { {} }, )); If you wish to initialize an attribute with a CODE reference itself, then you need to wrap that in a subroutine as well, like so: 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 a CODE reference, it is passed the (as yet unfinished) instance structure as it's only argument. So you can do things like this: Class::MOP::Attribute->new('$object_identity' => ( default => sub { Scalar::Util::refaddr($_[0]) }, )); This last feature is fairly limited as there is no gurantee of the order of attribute initializations, so you cannot perform any kind of dependent initializations. However, if this is something you need, you could subclass Class::MOP::Class and this class to acheive it. However, this is currently left as an exercise to the reader :). This may be a method name (referring to a method on the class with this attribute) or a CODE ref. The initializer is used to set the attribute value on an instance when the attribute is set during instance initialization. When called, it is passed the instance (as the invocant), the value to set, a slot-setting CODE ref, and the attribute meta-instance. The slot-setting code is provided to make it easy to set the (possibly altered) value on the instance without going through several more method calls. This contrived example shows an initializer that sets the attribute to twice the given value. Class::MOP::Attribute->new('$doubled' => ( initializer => sub { my ($instance, $value, $set) = @_; $set->($value * 2); }, )); As method names can be given as initializers, one can easily make attribute initialization use the writer: Class::MOP::Attribute->new('$some_attr' => ( writer => 'some_attr', initializer => 'some_attr', )); Your writer will simply need to examine it's @_ and determine under which context it is being called. The accessor, reader, writer, predicate and clearer keys can contain either; the name of the method and an appropriate default one will be generated for you, or a HASH ref containing exactly one key (which will be used as the name of the method) and one value, which should contain a CODE reference which will be installed as the method itself. The accessor is a standard perl-style read/write accessor. It will return the value of the attribute, and if a value is passed as an argument, it will assign that value to the attribute. NOTE: This method will properly handle the following code, by assigning an "undef" value to the attribute. $object->set_something(undef); This is a basic read-only accessor, it will just return the value of the attribute. This is a basic write accessor, it accepts a single argument, and assigns that value to the attribute. This method does not intentially return a value, however perl will return the result of the last expression in the subroutine, which returns in this returning the same value that it was passed. NOTE: This method will properly handle the following code, by assigning an "undef" value to the attribute. $object->set_something(); This is a basic test to see if any value has been set for the attribute. It will return true (1) if the attribute has been set to any value (even "undef"), and false (0) otherwise. NOTE: The predicate will return true even when you set an attribute's value to "undef". This behaviour has changed as of version 0.43. In older versions, the predicate (erroneously) checked for attribute value definedness, instead of presence as it is now. If you really want to get rid of the value, you have to define and use a clearer (see below). This is the a method that will uninitialize the attr, reverting lazy values back to their "unfulfilled" state. This will return a clone of the attribute instance, allowing the overriding of various attributes through the %options supplied. This method is used internally to initialize the approriate slot for this attribute in a given $instance, the $params passed are those that were passed to the constructor.

Value management
These methods are basically "backdoors" to the instance, which can be used to bypass the regular accessors, but still stay within the context of the MOP. These methods are not for general use, and should only be used if you really know what you are doing. Set the value without going through the accessor. Note that this may be done to even attributes with just read only accessors. This method sets the value without going through the accessor -- but it is only called when the instance data is first initialized. Return the value without going through the accessor. Note that this may be done even to attributes with just write only accessors. Return a boolean indicating if the item in the $instance has a value in it. This is basically what the default "predicate" method calls. This will clear the value in the $instance. This is basically what the default "clearer" would call. Note that this may be done even if the attirbute does not have any associated read, write or clear methods.

Informational
These are all basic read-only value accessors for the values passed into "new". I think they are pretty much self-explanitory. Return the default value for the attribute. If you pass in an $instance argument to this accessor and the default is a CODE reference, then the CODE reference will be executed with the $instance as its argument. Return a list of slots required by the attribute. This is usually just one, which is the name of the attribute. Return the name of a method name suitable for reading / writing the value of the attribute in the associated class. Suitable for use whether "reader" and "writer" or "accessor" was used. Return the CODE reference of a method suitable for reading / writing the value of the attribute in the associated class. Suitable for use whether "reader" and "writer" or "accessor" was specified or not. NOTE: If no reader/writer/accessor was specified, this will use the attribute get_value/set_value methods, which can be very inefficient. Return whether a method exists suitable for reading / writing the value of the attribute in the associated class. Suitable for use whether "reader" and "writer" or "accessor" was used.

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

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. This returns the metaclass this attribute is associated with. This will store a weaken reference to $class internally. You should note that just changing the class assocation will not remove the attribute from it's old class, and initialize it (and it's accessors) in the new $class. It is up to you to do this manually. This will remove the weakened reference to the class. It does not remove the attribute itself from the class (or remove it's accessors), you must do that yourself if you want too. Actually if that is what you want to do, you should probably be looking at Class::MOP::Class::remove_attribute instead.

Attribute Accessor generation
Accessors are generated by an accessor metaclass, which is usually a subclass of "Class::MOP::Method::Accessor". This method returns the name of the accessor metaclass that this attribute uses. This will associate a $method with the given attribute which is used internally by the accessor generator. This will return the list of methods which have been associated with the "associate_method" methods. This is a good way of seeing what methods are used to manage a given attribute. This allows the attribute to generate and install code for it's own accessor/reader/writer/predicate methods. This is called by "Class::MOP::Class::add_attribute". This method will call "process_accessors" for each of the possible method types (accessor, reader, writer & predicate). This takes a $type (accessor, reader, writer or predicate), and a $value (the value passed into the constructor for each of the different types). It will then either generate the method itself (using the "generate_*_method" methods listed below) or it will use the custom method passed through the constructor. This allows the attribute to remove the method for it's own accessor/reader/writer/predicate/clearer. This is called by "Class::MOP::Class::remove_attribute". NOTE: This does not currently remove methods from the list returned by "associated_methods", that is on the TODO list.

Introspection
This will return a Class::MOP::Class instance which is related to this class. It should also be noted that Class::MOP will actually bootstrap this module by installing a number of attribute meta-objects into it's metaclass. This will allow this class to reap all the benefits of the MOP when subclassing it. Stevan Little <stevan@iinteractive.com> Copyright 2006-2008 by Infinity Interactive, Inc. <http://www.iinteractive.com> This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.