DESCRIPTION

This module provides a semi-affordance style accessor builder for Class::Meta. Affordance accessors are attribute accessor methods that separate the getting and setting of an attribute value into distinct methods. The approach both eliminates the overhead of checking to see whether an accessor is called as a getter or a setter, which is common for Perl style accessors, while also creating a psychological barrier to accidentally misusing an attribute.

Accessors

Class::Meta::AccessorBuilder::SemiAffordance create two different types of accessors: getters and setters. What makes the accessors generated by this class "semi-affordance" rather than "affordance" accessors is that the getter is simply named for the attribute, while the setter is prepended by set_. This approach differs from that of affordance accessors, where the getter is prepended by get_.

The type of accessors created depends on the value of the authz attribute of the Class::Meta::Attribute for which the accessor is being created.

For example, if the authz is Class::Meta::RDWR, then two accessor methods will be created:

my $value = $obj->io_socket;
$obj->set_io_socket($value);

If the value of authz is Class::Meta::READ, then only the get method will be created:

my $value = $obj->io_socket;

And finally, if the value of authz is Class::Meta::WRITE, then only the set method will be created (why anyone would want this is beyond me, but I provide for the sake of completeness):

my $value = $obj->io_socket;

Data Type Validation

Class::Meta::AccessorBuilder::SemiAffordance uses all of the validation checks passed to it to validate new values before assigning them to an attribute. It also checks to see if the attribute is required, and if so, adds a check to ensure that its value is never undefined. It does not currently check to ensure that private and protected methods are used only in their appropriate contexts, but may do so in a future release.

Class Attributes

If the context attribute of the attribute object for which accessors are to be built is Class::Meta::CLASS, Class::Meta::AccessorBuilder will build accessors for a class attribute instead of an object attribute. Of course, this means that if you change the value of the class attribute in any context--whether via a an object, the class name, or an an inherited class name or object, the value will be changed everywhere.

For example, for a class attribute "count", you can expect the following to work:

Currently, class attribute accessors are not designed to be inheritable in the way designed by Class::Data::Inheritable, although this might be changed in a future release. For now, I expect that the current simple approach will cover the vast majority of circumstances.

Note: Class attribute accessors will not work accurately in multiprocess environments such as mod_perl. If you change a class attribute's value in one process, it will not be changed in any of the others. Furthermore, class attributes are not currently shared across threads. So if you're using Class::Meta class attributes in a multi-threaded environment (such as iThreads in Perl 5.8.0 and later) the changes to a class attribute in one thread will not be reflected in other threads.

Private and Protected Attributes

Any attributes that have their view attribute set to Class::Meta::Private or Class::Meta::Protected get additional validation installed to ensure that they're truly private and protected. This includes when they are set via parameters to constructors generated by Class::Meta. The validation is performed by checking the caller of the accessors, and throwing an exception when the caller isn't the class that owns the attribute (for private attributes) or when it doesn't inherit from the class that owns the attribute (for protected attributes).

As an implementation note, this validation is performed for parameters passed to constructors created by Class::Meta by ignoring looking for the first caller that isn't Class::Meta::Constructor:

This works because Class::Meta::Constructor installs the closures that become constructors, and thus, when those closures call accessors to set new values for attributes, the caller is Class::Meta::Constructor. By going up the stack until we find another package, we correctly check to see what context is setting attribute values via a constructor, rather than the constructor method itself being the context.

This is a bit of a hack, but since Perl uses call stacks for checking security in this way, it's the best I could come up with. Other suggestions welcome. Or see Class::Meta::Type to create your own accessor generation code

INTERFACE

The following functions must be implemented by any Class::Meta accessor generation module.

Functions

build_attr_get

This function is called by Class::Meta::Type::make_attr_get() and returns a code reference that can be used by the get() method of Class::Meta::Attribute to return the value stored for that attribute for the object passed to the code reference.

build_attr_set

This function is called by Class::Meta::Type::make_attr_set() and returns a code reference that can be used by the set() method of Class::Meta::Attribute to set the value stored for that attribute for the object passed to the code reference.