NAME

Class::Attrib - Abstract translucent attribute management.

SYNOPSIS

  • Defines a simple way to specify attribute default values per class.

  • Provides an inherited view of class attribute definitions.

  • AUTOLOAD's accessor methods for visible attributes only.

CLASS ATTRIBUTE DEFINITIONS

Example:

package MyApp::MyPackage;
use strict;

our @ISA = qw( Class::Attrib );

our %Attrib = (
	ClassAttrib		=> 12345,
	translucent_attrib	=> "foo"
	mandatory_attrib	=> undef,
);

1;

Explanation:

Attribute definitions are kept in hashes named 'Attrib' in the derived classes. The details of the attribute definitions determine the behavior of the accessor methods.

ClassAttrib (a class attribute) only has useful meaning during instantiation of an object, therefore instance data is ignored entirely during accessor calls.

translucent_attrib is an instance attribute. Instances inherit their value from their (possibly itself inherited) class default, unless an overriding value has been stored on the object itself.

mandatory_attrib (an object attribute) has an undefined default, therefore warnings will be issued when the program tries to access the attribute before the object sets a value.

CLASS ATTRIBUTE ACCESSOR METHOD

$this->Attrib();

Called without arguments, returns a hash containing all known attributes and their default values as inherited from the calling class. (TODO)

Returns a hash reference.

$this->Attrib( attribute );

Called with one argument, returns the default value of the named attribute as inherited by the calling class.

$this->Attrib( attribute, value );

Called with two arguments, overrides an existing attribute default value in the closest class that defined it at compile-time.

No mechanism is provided for defining new attributes after compilation.

Returns the newly assigned value, for convenience.

INSTANCE ATTRIBUTE ACCESSOR

All three forms act exactly as Attrib when called as a class method.

$this->attrib();

Returns a copy of all attribute values specific to the instance.

$self->attrib( attribute );

Returns the value of the named attribute. If the instance does not have a corresponding value set, the inherited default value is returned.

$self->attrib( attribute, value );

Sets the instance-specific value of an attribute. If the supplied value is 'undef', removes any previously stored instance-specific value.

ATTRIBUTE NAMED ACCESSOR METHODS

Each attribute has a corresponding accessor method with the same name.

$this->foo();

Equivalent to $this->attrib( 'foo' );.

$this->foo( value );

Equivalent to $this->attrib( 'foo', $value );.

LIMITATIONS

Storing references (blessed or otherwise) in an attribute won't ruffle any feathers in Class::Attrib itself, but could cause exceptions to be thrown if the composite class has a persistence mechanism.

Class::Attrib is an abstract class. It contains no constructors, therefore it cannot be instantiated without some impolite bless hackery.

AUTHORS

K Cody <kcody@jilcraft.com>