NAME
Object::Stash - provides a Catalyst-like "stash" method for your class
SYNOPSIS
{
package MyPerson;
use Object::New;
use Object::Stash::Util 'has_stash';
has_stash personal_data => (
isa => 'Object',
handles => {
name => { is => 'ro' },
age => { is => 'rw' },
mbox => { is => 'rw', default => sub { ... } },
},
);
}
my $bob = MyPerson->new;
$bob->personal_data(name => 'Bob', age => 21, likes => 'fish');
$bob->age++;
printf("%s is aged %d", $bob->name, $bob->age);
DESCRIPTION
has_stash
This module exists to provide a function has_stash
which is similar in spirit to the has
function provided by Moose (and Moose-like modules), however the attribute it creates:
is always read-only
is always a hashref (or a special blessed hashref)
is not initialised by the constructor
Like Moose's has
it takes a list of options, but only two options are currently supported:
isa - "HashRef" or "Object"
handles - can be a hashref like:
handles => { 'foo' => {}, 'bar' => { is => 'ro' }, 'baz' => {}, }
or an arrayref like:
handles => [ 'foo', 'bar' => { is => 'ro' }, 'baz', ]
or if you only want to handle one method, can just be a string:
handles => 'foo'
The "handles" stuff allows you to delegate certain methods from your class to the stash. Thus, given the package in the SYNOPSIS section above, the MyPerson
class has methods name
, age
and mbox
defined, which store their data inside the personal_data
stash.
Each delegated method has its own set of method options (like the is => 'ro'
stuff above). The following method options are currently supported:
is - "ro" (read only) or "rw" (read-write)
Note that the method being read-only doen't prevent the data being modified in other ways (not using the installed method).
default - value to use as the default value for the method (if the setter has not yet been called). If you provide a coderef here, then it will be executed and expected to return the default value. The default is set lazily.
slot - hash key to use when storing data in the stash. Defaults to the method name.
Future versions may add other Moose-inspired options here, such as isa
.
Lower-Level functions
BUGS
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Object-Stash.
SEE ALSO
AUTHOR
Toby Inkster <tobyink@cpan.org>.
COPYRIGHT AND LICENCE
This software is copyright (c) 2012 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.