The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::Attrib - Abstract translucent attribute management.

SYNOPSIS

  • Provides an inherited view of attributes.

  • AUTOLOAD's accessor methods for visible attributes only.

  • Supplies a simple way to specify attributes and default values.

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 class package.

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 has an undefined default, therefore warnings will be issued if the program tries to access the attribute before it sets a value on the object.

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. A closure is installed when first called to improve performance.

$this->foo();

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

$this->foo( value );

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

$this->Bar();

Equivalent to $this->Attrib( 'Bar' );

LIMITATIONS

Attribute values stored on the instance are actually stored within a Class::Attrib lexical; this avoids collisions and also removes the assumption of a hash. Class::Attrib works perfectly well on scalar and array classes as well. However, this also means that attributes are invisible to serializers.

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

Kevin Cody-Little <kcody@cpan.org>