DESCRIPTION

The Catalyst context object has a method called stash, that provides a hashref for storing arbitrary data associated with the object. This is arguably a little hackish - the proper solution might be to create a slot for each piece of information you wish to store, with appropriate accessor methods. But often hackish will do.

(And there are non-hackish ways of using Object::Stash. Take a look at Web::Magic which uses a private stash - named with a leading underscore - and provides public methods for accessing various things stored inside it.)

Object::Stash sets up one or more stash methods for your class. How these methods are named depends on how Object::Stash is imported. Object::Stash is a role, like Object::New or Object::ID. This means you import it, but don't inherit from it.

Default method name

package MyClass;
use Object::Stash;

Creates a single method for MyClass objects. The method is called "stash".

Custom method name

package MyClass;
use Object::Stash 'data';

Creates a single method for MyClass objects. The method is called "data".

Multiple methods

package MyClass;
use Object::Stash qw/important trivial/;

Creates two stashes for MyClass objects, called "important" and "trivial". Adding data to one stash will not affect the other stash. You could alternatively write:

Stash Storage

Stashes are stored "inside-out", meaning that they will work not only with objects which are blessed hashrefs, but also with any other type of object internals. Dumping your object with Data::Dumper or similar will not display the contents of the stashes. (A future release of this module may introduce other storage options, but the current inside-out storage is likely to remain the default.)

Thanks to Hash::FieldHash, an object's stashes should get automatically garbage collected once the object itself is destroyed, unless you've maintained your own references to the stashes.

Stash Objects

While stashes are usually hashrefs, there is also an option to make stashes themselves blessed objects. It's best to illustrate this with an example