start page | rating of books | rating of authors | reviews | copyrights

Programming Perl

Programming PerlSearch this book
Previous: 7.2.71 Text::Wrap - Wrap Text into a Paragraph Chapter 7
The Standard Perl Library
Next: 7.2.73 Tie::Scalar, Tie::StdScalar - Base Class Definitions for Tied Scalars
 

7.2.72 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 Chapter 5 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. ( Chapter 5 not only documents these methods, but also has sample code.)

TIEHASH ClassName , LIST

The method invoked by the command:

tie %hash, 

ClassName

, 

LIST

Associates a new hash instance with the specified class. LIST would represent additional arguments (along the lines of AnyDBM_File and compatriots) needed to complete the association.

STORE this, key, value

Store value into key for the tied hash this .

FETCH this, key

Retrieve the value associated with key for the tied hash this .

FIRSTKEY this

Return the key/value pair for the first key in hash this .

NEXTKEY this, lastkey

Return the next key/value pair for the hash.

EXISTS this, key

Verify that key exists with the tied hash this .

DELETE this, key

Delete key from the tied hash this .

CLEAR this

Clear all values from the tied hash this .

Chapter 5 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.

7.2.72.1 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.


Previous: 7.2.71 Text::Wrap - Wrap Text into a Paragraph Programming Perl Next: 7.2.73 Tie::Scalar, Tie::StdScalar - Base Class Definitions for Tied Scalars
7.2.71 Text::Wrap - Wrap Text into a Paragraph Book Index 7.2.73 Tie::Scalar, Tie::StdScalar - Base Class Definitions for Tied Scalars