Perl version

Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tie::Hash

Perl 5 version 12.0 documentation
Recently read

Tie::Hash

NAME

Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes

SYNOPSIS

  1. package NewHash;
  2. require Tie::Hash;
  3. @ISA = qw(Tie::Hash);
  4. sub DELETE { ... } # Provides needed method
  5. sub CLEAR { ... } # Overrides inherited method
  6. package NewStdHash;
  7. require Tie::Hash;
  8. @ISA = qw(Tie::StdHash);
  9. # All methods provided by default, define only those needing overrides
  10. # Accessors access the storage in %{$_[0]};
  11. # TIEHASH should return a reference to the actual storage
  12. sub DELETE { ... }
  13. package NewExtraHash;
  14. require Tie::Hash;
  15. @ISA = qw(Tie::ExtraHash);
  16. # All methods provided by default, define only those needing overrides
  17. # Accessors access the storage in %{$_[0][0]};
  18. # TIEHASH should return an array reference with the first element being
  19. # the reference to the actual storage
  20. sub DELETE {
  21. $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
  22. delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1])
  23. }
  24. package main;
  25. tie %new_hash, 'NewHash';
  26. tie %new_std_hash, 'NewStdHash';
  27. tie %new_extra_hash, 'NewExtraHash',
  28. sub {warn "Doing \U$_[1]\E of $_[2].\n"};

DESCRIPTION

This module provides some skeletal methods for hash-tying classes. See perltie 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 and Tie::ExtraHash packages provide most methods for hashes described in perltie (the exceptions are UNTIE and DESTROY ). They cause tied hashes to behave exactly like standard hashes, and allow for selective overwriting of methods. Tie::Hash grandfathers the new method: it is used if TIEHASH is not defined in the 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. See the perltie section for more detailed descriptive, as well as example code:

  • TIEHASH classname, LIST

    The method invoked by the command tie %hash, classname . 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 datum value into key for the tied hash this.

  • FETCH this, key

    Retrieve the datum in key for the tied hash this.

  • FIRSTKEY this

    Return the first key in the hash.

  • NEXTKEY this, lastkey

    Return the next key in the hash.

  • EXISTS this, key

    Verify that key exists with the tied hash this.

    The Tie::Hash implementation is a stub that simply croaks.

  • DELETE this, key

    Delete the key key from the tied hash this.

  • CLEAR this

    Clear all values from the tied hash this.

  • SCALAR this

    Returns what evaluating the hash in scalar context yields.

    Tie::Hash does not implement this method (but Tie::StdHash and Tie::ExtraHash do).

Inheriting from Tie::StdHash

The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by tied(%tiedhash). Thus overwritten TIEHASH method should return a hash reference, and the remaining methods should operate on the hash referenced by the first argument:

  1. package ReportHash;
  2. our @ISA = 'Tie::StdHash';
  3. sub TIEHASH {
  4. my $storage = bless {}, shift;
  5. warn "New ReportHash created, stored in $storage.\n";
  6. $storage
  7. }
  8. sub STORE {
  9. warn "Storing data with key $_[1] at $_[0].\n";
  10. $_[0]{$_[1]} = $_[2]
  11. }

Inheriting from Tie::ExtraHash

The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by (tied(%tiedhash))->[0] . Thus overwritten TIEHASH method should return an array reference with the first element being a hash reference, and the remaining methods should operate on the hash %{ $_[0]->[0] } :

  1. package ReportHash;
  2. our @ISA = 'Tie::ExtraHash';
  3. sub TIEHASH {
  4. my $class = shift;
  5. my $storage = bless [{}, @_], $class;
  6. warn "New ReportHash created, stored in $storage.\n";
  7. $storage;
  8. }
  9. sub STORE {
  10. warn "Storing data with key $_[1] at $_[0].\n";
  11. $_[0][0]{$_[1]} = $_[2]
  12. }

The default TIEHASH method stores "extra" arguments to tie() starting from offset 1 in the array referenced by tied(%tiedhash); this is the same storage algorithm as in TIEHASH subroutine above. Hence, a typical package inheriting from Tie::ExtraHash does not need to overwrite this method.

SCALAR , UNTIE and DESTROY

The methods UNTIE and DESTROY are not defined in Tie::Hash, Tie::StdHash, or Tie::ExtraHash. Tied hashes do not require presence of these methods, but if defined, the methods will be called in proper time, see perltie.

SCALAR is only defined in Tie::StdHash and Tie::ExtraHash.

If needed, these methods should be defined by the package inheriting from Tie::Hash, Tie::StdHash, or Tie::ExtraHash. See SCALAR in perltie to find out what happens when SCALAR does not exist.

MORE INFORMATION

The packages relating to various DBM-related implementations (DB_File, NDBM_File, etc.) show examples of general tied hashes, as does the Config module. While these do not utilize Tie::Hash, they serve as good working examples.