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.
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:
$name
%options
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.
-foo
MyClass->meta->construct_instance(-foo => "Hello There");
In an init_arg is not assigned, it will automatically use the value of $name.
The value of this key is the default value which Class::MOP::Class::construct_instance will initialize the attribute to.
Class::MOP::Class::construct_instance
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 and predicate 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.
undef
$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.
$object->set_something();
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.
1
0
These are all basic read-only value accessors for the values passed into new. I think they are pretty much self-explanitory.
new
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.
$instance
These are all basic predicate methods for the values passed into new.
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.
Class::MOP::Class::add_attribute
This method will call process_accessors for each of the possible method types (accessor, reader, writer & predicate).
process_accessors
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.
$type
$value
generate_*_method
This allows the attribute to remove the method for it's own accessor/reader/writer/predicate. This is called by Class::MOP::Class::remove_attribute.
Class::MOP::Class::remove_attribute
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.
Stevan Little <stevan@iinteractive.com>
Copyright 2006 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.
To install Class::MOP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::MOP
CPAN shell
perl -MCPAN -e shell install Class::MOP
For more information on module installation, please visit the detailed CPAN module installation guide.