Mixin::ExtraFields::Driver::HashGuts(3) store extras in a hashy object's guts


version 0.140001


package Your::HashBased::Class;
use Mixin::ExtraFields -fields => { driver => 'HashGuts' };


This driver class implements an extremely simple storage mechanism: extras are stored on the object on which the mixed-in methods are called. By default, they are stored under the key returned by the "default_has_key" method, but this can be changed by providing a "hash_key" argument to the driver configuration, like so:

  use Mixin::ExtraFields -fields => {
    driver => { class => 'HashGuts', hash_key => "\0Something\0Wicked\0" }


In addition to the methods required by Mixin::ExtraFields::Driver, the following methods are provided:


  my $key = $driver->hash_key;

This method returns the key where the driver will store its extras.


If no "hash_key" argument is given for the driver, this method is called during driver initialization. It will return a unique string to be used as the hash key.


This method returns the hashref of storage used for extras. Individual objects get weak references to their id within this hashref.


  my $stash = $driver->storage_for($object, $id);

This method returns the hashref to use to store extras for the given object and id. This hashref is stored on both the hash-based object (in its "hash_key" entry) and on the driver (in the entry for $id in its "storage" hash).

All objects with the same id should end up with the same hash in their "hash_key" field. None of these references are weakened, which means two things: first, even if all objects with a given id go out of scope, future objects with that id will retain the original extras; secondly, memory used to store extras is never reclaimed. If this is a problem, use a more sophisticated driver.


This code was written by Ricardo SIGNES. His code in 2006 was sponsored by Listbox.


Copyright (C) 2006, Ricardo SIGNES. This code is free software, and is available under the same terms as perl itself.


Ricardo Signes <[email protected]>


This software is copyright (c) 2013 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.