Tie::Hash, Tie::StdHash - Base Class Definitions for Tied Hashes
package NewHash; require Tie::Hash; @ISA = (Tie::Hash); sub DELETE {
...
}
# Provides additional method sub CLEAR {
...
}
# Overrides inherited method package NewStdHash; require Tie::Hash; @ISA = (Tie::StdHash); sub DELETE {
...
}
package main; tie %new_hash, "NewHash"; tie %new_std_hash, "NewStdHash";
This module provides some skeletal methods for hash-tying classes. (See for a list of the functions required in order to tie a hash to a package.) The basic Tie::Hash package provides a new() method, as well as methods TIEHASH(), EXISTS() and CLEAR(). The Tie::StdHash package provides most methods required for hashes. It inherits from Tie::Hash, and causes tied hashes to behave exactly like standard hashes, allowing for selective overloading of methods. The new() method is provided as grandfathering in case a class forgets to include a TIEHASH() method.
For developers wishing to write their own tied hashes, the required methods are briefly defined below. ( not only documents these methods, but also has sample code.)
TIEHASHClassName,LIST- The method invoked by the command:
tie %hash,
ClassName,LISTAssociates a new hash instance with the specified class.
LISTwould represent additional arguments (along the lines of AnyDBM_File and compatriots) needed to complete the association. STOREthis, key, value- Store
valueintokeyfor the tied hashthis. FETCHthis, key- Retrieve the value associated with
keyfor the tied hashthis. FIRSTKEYthis- Return the key/value pair for the first key in hash
this. NEXTKEYthis, lastkey- Return the next key/value pair for the hash.
EXISTSthis, key- Verify that
keyexists with the tied hashthis. DELETEthis, key- Delete
keyfrom the tied hashthis. CLEARthis- Clear all values from the tied hash
this.
includes a method called DESTROY() as a "necessary" method for tied hashes. However, it is not actually required, and neither Tie::Hash nor Tie::StdHash defines a default for this method.
See also
The library modules relating to various DBM-related implementations (DB_File, GDBM_File, NDBM_File, ODBM_File, and SDBM_File) show examples of general tied hashes, as does the Config module. While these modules do not utilize Tie::Hash, they serve as good working examples.