[Chapter 7] 7.2.72 Tie::Hash, Tie::StdHash - Base Class Definitions for Tied Hashes
Author: Larry Wall, Tom Christiansen & Randal L. Schwartz
Date: 1999-07-02T02:04:45Z

#

| Chapter 7 The Standard Perl Library |

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

7.2.71 Text::Wrap - Wrap Text into a Paragraph |  | 7.2.73 Tie::Scalar, Tie::StdScalar - Base Class Definitions for Tied Scalars