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

NAME

Object::Accessor

SYNOPSIS

    ### using the object
    $object = Object::Accessor->new;        # create object
    $object = Object::Accessor->new(@list); # create object with accessors
    $object = Object::Accessor->new(\%h);   # create object with accessors
                                            # and their allow handlers

    $bool   = $object->mk_accessors('foo'); # create accessors
    $bool   = $object->mk_accessors(        # create accessors with input
                { foo => ALLOW_HANDLER } ); # validation
                
    $clone  = $object->mk_clone;            # create a clone of original
                                            # object without data
    $bool   = $object->mk_flush;            # clean out all data

    @list   = $object->ls_accessors;        # retrieves a list of all
                                            # accessors for this object

    $bar    = $object->foo('bar');          # set 'foo' to 'bar'
    $bar    = $object->foo();               # retrieve 'bar' again

    $sub    = $object->can('foo');          # retrieve coderef for
                                            # 'foo' accessor
    $bar    = $sub->('bar');                # set 'foo' via coderef
    $bar    = $sub->();                     # retrieve 'bar' by coderef

    ### using the object as base class
    package My::Class;
    use base 'Object::Accessor';

    $object     = My::Class->new;               # create base object
    $bool       = $object->mk_accessors('foo'); # create accessors, etc...

    ### make all attempted access to non-existant accessors fatal
    ### (defaults to false)
    $Object::Accessor::FATAL = 1;

    ### enable debugging
    $Object::Accessor::DEBUG = 1;


    ### advanced usage -- scoped attribute values
    {   my $obj = Object::Accessor->new;
        
        $obj->mk_accessors('foo');
        $obj->foo( 1 );
        print $obj->foo;                # will print 1

        ### bind the scope of the value of attribute 'foo'
        ### to the scope of '$x' -- when $x goes out of 
        ### scope, 'foo's previous value will be restored
        {   $obj->foo( 2 => \my $x );
            print $obj->foo, ' ', $x;   # will print '2 2'
        }
        print $obj->foo;                # will print 1
    }

DESCRIPTION

Object::Accessor provides an interface to create per object accessors (as opposed to per Class accessors, as, for example, Class::Accessor provides).

You can choose to either subclass this module, and thus using its accessors on your own module, or to store an Object::Accessor object inside your own object, and access the accessors from there. See the SYNOPSIS for examples.

METHODS

$object = Object::Accessor->new( [ARGS] );

Creates a new (and empty) Object::Accessor object. This method is inheritable.

Any arguments given to new are passed straight to mk_accessors.

$bool = $object->mk_accessors( @ACCESSORS | \%ACCESSOR_MAP );

Creates a list of accessors for this object (and NOT for other ones in the same class!). Will not clobber existing data, so if an accessor already exists, requesting to create again is effectively a no-op.

When providing a hashref as argument, rather than a normal list, you can specify a list of key/value pairs of accessors and their respective input validators. The validators can be anything that Params::Check's allow function accepts. Please see its manpage for details.

For example:

    $object->mk_accessors( {
        foo     => qr/^\d+$/,       # digits only
        bar     => [0,1],           # booleans
        zot     => \&my_sub         # a custom verification sub
    } );        

Returns true on success, false on failure.

Accessors that are called on an object, that do not exist return undef by default, but you can make this a fatal error by setting the global variable $FATAL to true. See the section on GLOBAL VARIABLES for details.

Note that you can bind the values of attributes to a scope. This allows you to temporarily change a value of an attribute, and have it's original value restored up on the end of it's bound variable's scope;

For example, in this snippet of code, the attribute foo will temporarily be set to 2, until the end of the scope of $x, at which point the original value of 1 will be restored.

    my $obj = Object::Accessor->new;
    
    $obj->mk_accessors('foo');
    $obj->foo( 1 );
    print $obj->foo;                # will print 1

    ### bind the scope of the value of attribute 'foo'
    ### to the scope of '$x' -- when $x goes out of 
    ### scope, 'foo' previous value will be restored
    {   $obj->foo( 2 => \my $x );
        print $obj->foo, ' ', $x;   # will print '2 2'
    }
    print $obj->foo;                # will print 1
    

Note that all accessors are read/write for everyone. See the TODO section for details.

@list = $self->ls_accessors;

Returns a list of accessors that are supported by the current object. The corresponding coderefs can be retrieved by passing this list one by one to the can method.

$ref = $self->ls_allow(KEY)

Returns the allow handler for the given key, which can be used with Params::Check's allow() handler.

$clone = $self->mk_clone;

Makes a clone of the current object, which will have the exact same accessors as the current object, but without the data stored in them.

$bool = $self->mk_flush;

Flushes all the data from the current object; all accessors will be set back to their default state of undef.

Returns true on success and false on failure.

$bool = $self->mk_verify;

Checks if all values in the current object are in accordance with their own allow handler. Specifically useful to check if an empty initialised object has been filled with values satisfying their own allow criteria.

$bool = $self->can( METHOD_NAME )

This method overrides UNIVERAL::can in order to provide coderefs to accessors which are loaded on demand. It will behave just like UNIVERSAL::can where it can -- returning a class method if it exists, or a closure pointing to a valid accessor of this particular object.

You can use it as follows:

    $sub = $object->can('some_accessor');   # retrieve the coderef
    $sub->('foo');                          # 'some_accessor' now set
                                            # to 'foo' for $object
    $foo = $sub->();                        # retrieve the contents
                                            # of 'some_accessor'

See the SYNOPSIS for more examples.

GLOBAL VARIABLES

$Object::Accessor::FATAL

Set this variable to true to make all attempted access to non-existant accessors be fatal. This defaults to false.

$Object::Accessor::DEBUG

Set this variable to enable debugging output. This defaults to false.

TODO

Create read-only accessors

Currently all accessors are read/write for everyone. Perhaps a future release should make it possible to have read-only accessors as well.

CAVEATS

If you use codereferences for your allow handlers, you will not be able to freeze the data structures using Storable.

Due to a bug in storable (until at least version 2.15), qr// compiled regexes also don't de-serialize properly. Although this bug has been reported, you should be aware of this issue when serializing your objects.

AUTHOR

This module by Jos Boumans <kane@cpan.org>.

COPYRIGHT

This module is copyright (c) 2004-2005 Jos Boumans <kane@cpan.org>. All rights reserved.

This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.