NAME

Tie::Hash::Layered - Perl extension for layerable hash values

SYNOPSIS

 use Tie::Hash::Layered;
 

 my %hash;
 my %test1 = ( foo => 'bar', bob => 'sprite' );
 my %test2 = ( bob => 'joey');

 # tie the new hash with the initialised hashes above
 tie %hash, 'Tie::Hash::Layered', (\%test1, \%test2);

 # because the hash values are layered 
 # left to right is bottom to top so ...
 # $hash{'bob'} eq 'joey' 
 # $hash{'foo'} eq 'bar' 
 # ... which is pretty cool

 $hash{'foo'} = 'flam';
 # this sets 'foo' in the top most layer so ...
 # $hash{'foo'} now eq 'flam' 

 delete $hash{'bob'};

 # this deletes $hash{'bob'} in the top layer so ...
 # $hash{'bob'} now eq 'sprite'

 # let's clear the hash 
 %hash = ();

 # which clears the top layer so that ..
 # $hash{'foo'} now eq 'bar'

 # set foo and quux in the top layer
 $hash{'foo'} = 'flam';
 $hash{'quux'} = 'fleeg';
 
 # the keys of %hash are now ... 
 # foo, bob and  quux notice the lack of duplicates

 # setting mutt in the bottom later hash ...
 $test1{'mutt'} = 'ley';
 # ... also sets it in %hash
 # so $hash{'mutt'} eq 'ley'

 $test2{'mutt'} = 'mail';
 # and $hash{'mutt'} now eq 'mail'

 # you can access the stack of hashes 
 # like a normal array ...
 
 tied(%hash}->push( { slub => 'slob' } );
 # $hash{'slub'} eq 'slob'

 tied(%hash)->unshift( { slub => 'slab' } );
 # $hash{'slub'} eq 'slab'

 tied(%hash}->shift();
 # $hash{'slub'} eq 'slob'

 tied(%hash)->pop();
 # $hash{'slub'} is now not defined

 

DESCRIPTION

This module lets you layer hashes on top of each other opaquely so that top most layers obscure bottom ones.

Think of it like sheets of OHP transparencies, if a value is set in a top and bottom layer then that's you when you access that key you get the value from the top layer but if you access something not set in the top layer but set in the bottom layer then you get the value from the bottom layer ...

... and breathe.

In short :

Tied Hash : foo=>'bob' , quux=>'fleeg' | | ^ | Layer 1 : foo=>'bob' ^ Layer 2 : foo=>'bar' , quux=>'fleeg'

So why is this useful? Well, the obvious application is for preferences. In a CGI app you could tie in the bottom most hash to a database with default all-users' preferences, the second layer with the current user's preferences, the layer above that with the per-session preferences and the layer above that with the per-request values.

AUTHOR

Simon Wistow <simon@twoshortplanks.com>

COPYING

This is distributed under the same terms as Perl itself

SEE ALSO

perl, Tie::Hash