1Tie::Hash(3pm)         Perl Programmers Reference Guide         Tie::Hash(3pm)
2
3
4

NAME

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

SYNOPSIS

10           package NewHash;
11           require Tie::Hash;
12
13           @ISA = qw(Tie::Hash);
14
15           sub DELETE { ... }          # Provides needed method
16           sub CLEAR { ... }           # Overrides inherited method
17
18
19           package NewStdHash;
20           require Tie::Hash;
21
22           @ISA = qw(Tie::StdHash);
23
24           # All methods provided by default, define only those needing overrides
25           # Accessors access the storage in %{$_[0]};
26           # TIEHASH should return a reference to the actual storage
27           sub DELETE { ... }
28
29           package NewExtraHash;
30           require Tie::Hash;
31
32           @ISA = qw(Tie::ExtraHash);
33
34           # All methods provided by default, define only those needing overrides
35           # Accessors access the storage in %{$_[0][0]};
36           # TIEHASH should return an array reference with the first element being
37           # the reference to the actual storage
38           sub DELETE {
39             $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
40             delete $_[0][0]->{$_[1]};           #  $_[0]->SUPER::DELETE($_[1])
41           }
42
43
44           package main;
45
46           tie %new_hash, 'NewHash';
47           tie %new_std_hash, 'NewStdHash';
48           tie %new_extra_hash, 'NewExtraHash',
49               sub {warn "Doing \U$_[1]\E of $_[2].\n"};
50

DESCRIPTION

52       This module provides some skeletal methods for hash-tying classes. See
53       perltie for a list of the functions required in order to tie a hash to
54       a package. The basic Tie::Hash package provides a "new" method, as well
55       as methods "TIEHASH", "EXISTS" and "CLEAR". The Tie::StdHash and
56       Tie::ExtraHash packages provide most methods for hashes described in
57       perltie (the exceptions are "UNTIE" and "DESTROY").  They cause tied
58       hashes to behave exactly like standard hashes, and allow for selective
59       overwriting of methods.  Tie::Hash grandfathers the "new" method: it is
60       used if "TIEHASH" is not defined in the case a class forgets to include
61       a "TIEHASH" method.
62
63       For developers wishing to write their own tied hashes, the required
64       methods are briefly defined below. See the perltie section for more
65       detailed descriptive, as well as example code:
66
67       TIEHASH classname, LIST
68           The method invoked by the command "tie %hash, classname".
69           Associates a new hash instance with the specified class. "LIST"
70           would represent additional arguments (along the lines of
71           AnyDBM_File and compatriots) needed to complete the association.
72
73       STORE this, key, value
74           Store datum value into key for the tied hash this.
75
76       FETCH this, key
77           Retrieve the datum in key for the tied hash this.
78
79       FIRSTKEY this
80           Return the first key in the hash.
81
82       NEXTKEY this, lastkey
83           Return the next key in the hash.
84
85       EXISTS this, key
86           Verify that key exists with the tied hash this.
87
88           The Tie::Hash implementation is a stub that simply croaks.
89
90       DELETE this, key
91           Delete the key key from the tied hash this.
92
93       CLEAR this
94           Clear all values from the tied hash this.
95
96       SCALAR this
97           Returns what evaluating the hash in scalar context yields.
98
99           Tie::Hash does not implement this method (but Tie::StdHash and
100           Tie::ExtraHash do).
101

Inheriting from Tie::StdHash

103       The accessor methods assume that the actual storage for the data in the
104       tied hash is in the hash referenced by "tied(%tiedhash)".  Thus
105       overwritten "TIEHASH" method should return a hash reference, and the
106       remaining methods should operate on the hash referenced by the first
107       argument:
108
109         package ReportHash;
110         our @ISA = 'Tie::StdHash';
111
112         sub TIEHASH  {
113           my $storage = bless {}, shift;
114           warn "New ReportHash created, stored in $storage.\n";
115           $storage
116         }
117         sub STORE    {
118           warn "Storing data with key $_[1] at $_[0].\n";
119           $_[0]{$_[1]} = $_[2]
120         }
121

Inheriting from Tie::ExtraHash

123       The accessor methods assume that the actual storage for the data in the
124       tied hash is in the hash referenced by "(tied(%tiedhash))->[0]".  Thus
125       overwritten "TIEHASH" method should return an array reference with the
126       first element being a hash reference, and the remaining methods should
127       operate on the hash "%{ $_[0]->[0] }":
128
129         package ReportHash;
130         our @ISA = 'Tie::ExtraHash';
131
132         sub TIEHASH  {
133           my $class = shift;
134           my $storage = bless [{}, @_], $class;
135           warn "New ReportHash created, stored in $storage.\n";
136           $storage;
137         }
138         sub STORE    {
139           warn "Storing data with key $_[1] at $_[0].\n";
140           $_[0][0]{$_[1]} = $_[2]
141         }
142
143       The default "TIEHASH" method stores "extra" arguments to tie() starting
144       from offset 1 in the array referenced by "tied(%tiedhash)"; this is the
145       same storage algorithm as in TIEHASH subroutine above.  Hence, a
146       typical package inheriting from Tie::ExtraHash does not need to
147       overwrite this method.
148

"SCALAR", "UNTIE" and "DESTROY"

150       The methods "UNTIE" and "DESTROY" are not defined in Tie::Hash,
151       Tie::StdHash, or Tie::ExtraHash.  Tied hashes do not require presence
152       of these methods, but if defined, the methods will be called in proper
153       time, see perltie.
154
155       "SCALAR" is only defined in Tie::StdHash and Tie::ExtraHash.
156
157       If needed, these methods should be defined by the package inheriting
158       from Tie::Hash, Tie::StdHash, or Tie::ExtraHash. See "SCALAR" in
159       perltie to find out what happens when "SCALAR" does not exist.
160

MORE INFORMATION

162       The packages relating to various DBM-related implementations (DB_File,
163       NDBM_File, etc.) show examples of general tied hashes, as does the
164       Config module. While these do not utilize Tie::Hash, they serve as good
165       working examples.
166
167
168
169perl v5.10.1                      2009-02-12                    Tie::Hash(3pm)
Impressum