NAME
Tie::Hash::Stack - Maintains an array of hashes like a stack.
SYNOPSIS
use Tie::Hash::Stack qw(pop_hash push_hash merge_hash);
my %hash;
tie( %hash, "Tie::Hash::Stack" ); # Ties the hash
$hash{ 1 } = "one";
$hash{ 2 } = "two";
$hash{ 3 } = "three";
push_hash %hash; # Pushes a new hash on the stack
$hash{ 2 } = "II"; # $hash{ 2 } now 'II'
$hash{ 4 } = "IV";
push_hash %hash;
$hash{ 3 } = "9/3"; # $hash{ 3 } now '9/3'
$hash{ 5 } = "10/2";
pop_hash %hash; # $hash{ 3 } now 'three';
delete $hash{ 2 }; # $hash{ 2 } now undef'ed;
my %merged = merge_hash %hash; # ( 1=>one, 3=>three, 4=>IV )
DESCRIPTION
Tie::Hash::Stack
allows one to tie a hash to a data structure that is composed of an ordered (FILO) sequence of hashes; hash values are always set on the newest hash of the stack, and are retrieved from the hash that contains the requested that is newest on the stack. The stack can be manipulated to add or remove these hashes. This type of structure is good when one is collecting data in stages with the possibility of having to "back up" to previous stages.
In cases where the same key is in two or more hashes on the stack, the value from the most recent hash containing that key will be returned.
When tie
'd, the new hash will already have one hash on the stack, so that it can be used without any extra code. Optionally, a hash reference may be passed which will be used as the initial hash within the array.
Besides the standard hash functions, there are several functions that can be accessed through the tie'd hash variable.
push_hash
-
Pushes an empty hash onto the hash; any further hash assignments will be placed into this hash until it is removed, or a new hash pushed onto the stack. A optional second argument may be a reference to a hash which will be pushed onto the stack instead of an empty one.
pop_hash
-
Removes the last hash on the stack after deleting the values from it. If all hashes are removed, the stack is reset with an empty hash.
shift_hash
,unshift_hash
-
Complimentary functions of
pop_hash
andpush_hash
; removes the hash at the front of the stack, or adds a new hash to the front of the stack.unshift_hash
also takes a similar optional second argument as withpush_hash
. merge_hash
-
Merges all the hashes into one and returns this new (untied) hash; duplicated keys are treated as described above. The hash stack is left untouched.
flatten_hash
-
This is similar to
merge_hash
, but in this case, the merged hash is then set as the only hash on the stack for this object, all other data being thrown away. No return value is returned in this case. reverse_hash
-
Flips the order of the hashes around.
get_depth
-
Returns the number of hashes currently on the stack.
HISTORY
$Date: 2001/06/30 12:13:46 $
$Log: Stack.pm,v $
Revision 0.9 2001/06/30 12:13:46 mneylon
Initial Release (based on www.perlmonks.org code with some additional
changes)
AUTHOR
This package was written by Michael K. Neylon
COPYRIGHT
Copyright 2001 by Michael K. Neylon
LICENSE
This program is Copyright 2001 by Michael K. Neylon.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.