Object::Accessor
###->new('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 }
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.::Acccessor::Lvalue namespace instead. See the section on
LVALUE ACCESSORS below.
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..
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.
Method to directly access the value of the given accessor in the object. It circumvents all calls to allow checks, callbakcs, etc.
Use only if you
Know What You Are Doing! General usage for this functionality would be in your own custom callbacks.
Method to directly set the value of the given accessor in the object. It circumvents all calls to allow checks, callbakcs,.
Due to the nature of
lvalue subs, we never get access to the value you are assigning, so we can not check it againt your allow handler. Allow handlers are therefor unsupported under
lvalue conditions.
See
perldoc perlsub for details..