NAME
Class::MOP::Attribute - Attribute Meta Object
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 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.
METHODS
Creation
- new ($name, ?%options)
-
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:- init_arg
-
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
. - default
-
The value of this key is the default value which
Class::MOP::Class::construct_instance
will initialize the attribute to. - builder
-
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.
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 raw (unblessed) 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 :).
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.
- accessor
-
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);
- reader
-
This is a basic read-only accessor, it will just return the value of the attribute.
- writer
-
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();
- predicate
-
This is a basic test to see if the value of the attribute is not
undef
. It will return true (1
) if the attribute's value is defined, and false (0
) otherwise. - clearer
-
This is the a method that will uninitialize the attr, reverting lazy values back to their "unfulfilled" state.
- clone (%options)
- initialize_instance_slot ($instance, $params)
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_value ($instance, $value)
-
Set the value without going through the accessor. Note that this may be done to even attributes with just read only accessors.
- get_value ($instance)
-
Return the value without going through the accessor. Note that this may be done even to attributes with just write only accessors.
- has_value ($instance)
-
Returns a boolean indicating if the item in the
$instance
has a value in it. This is basically what the defaultpredicate
method calls. - clear_value ($instance)
-
This will clear the value in the
$instance
. This is basically what the defaultclearer
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.
- name
- accessor
- reader
- writer
- predicate
- clearer
- init_arg
- is_default_a_coderef
- default (?$instance)
-
As noted in the documentation for
new
above, if the default value is a CODE reference, this accessor will pass a single additional argument$instance
into it and return the value. - slots
-
Returns a list of slots required by the attribute. This is usually just one, which is the name of the attribute.
- get_read_method
- get_write_method
-
Return the name of a method suitable for reading / writing the value of the attribute in the associated class. Suitable for use whether
reader
andwriter
oraccessor
was used.
Informational predicates
These are all basic predicate methods for the values passed into new
.
- has_accessor
- has_reader
- has_writer
- has_predicate
- has_clearer
- has_init_arg
- has_default
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.
- associated_class
-
This returns the metaclass this attribute is associated with.
- attach_to_class ($class)
-
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. - detach_from_class
-
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
- accessor_metaclass
-
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. - associate_method ($method)
-
This will associate a
$method
with the given attribute which is used internally by the accessor generator. - associated_methods
-
This will return the list of methods which have been associated with the
associate_method
methods. - install_accessors
-
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). - process_accessors ($type, $value)
-
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 thegenerate_*_method
methods listed below) or it will use the custom method passed through the constructor. - remove_accessors
-
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
- meta
-
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 benifits of the MOP when subclassing it.
AUTHORS
Stevan Little <stevan@iinteractive.com>
COPYRIGHT AND LICENSE
Copyright 2006, 2007 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.