1Tie::Hash::Method(3) User Contributed Perl Documentation Tie::Hash::Method(3)
2
3
4
6 Tie::Hash::Method - Tied hash with specific methods overriden by
7 callbacks
8
10 Version 0.02
11
13 tie my %hash, 'Tie::Hash::Method',
14 FETCH => sub {
15 exists $_[0]->base_hash->{$_[1]} ? $_[0]->base_hash->{$_[1]} : $_[1]
16 };
17
19 Tie::Hash::Method provides a way to create a tied hash with specific
20 overriden behaviour without having to create a new class to do it. A
21 tied hash with no methods overriden is functionally equivalent to a
22 normal hash.
23
24 Each method in a standard tie can be overriden by providing a callback
25 to the tie call. So for instance if you wanted a tied hash that changed
26 'foo' into 'bar' on store you could say:
27
28 tie my %hash, 'Tie::Hash::Method',
29 STORE => sub {
30 (my $v=pop)=~s/foo/bar/g if defined $_[2];
31 return $_[0]->base_hash->{$_[1]}=$v;
32 };
33
34 The callback is called with exactly the same arguments as the tie
35 itself, in particular the tied object is always passed as the first
36 argument.
37
38 The tied object is itself an array, which contains a second hash in the
39 HASH slot (index 0) which is used to perform the default operations.
40
41 The callbacks available are in a hash keyed by name in the METHOD slot
42 of the array (index 1).
43
44 If your code needs to store extra data in the object it should be
45 stored in the PRIVATE slot of the object (index 2). No future release
46 of this module will ever use or alter anything in that slot.
47
48 The arguments passed to the tie constructor will be seperated by the
49 case of their keys. The ones with all capitals will be stored in the
50 METHOD hash, and the rest will be stored in the PRIVATE hash.
51
52 CALLBACKS
53 STORE this, key, value
54 Store datum value into key for the tied hash this.
55
56 FETCH this, key
57 Retrieve the datum in key for the tied hash this.
58
59 FIRSTKEY this
60 Return the first key in the hash.
61
62 NEXTKEY this, lastkey
63 Return the next key in the hash.
64
65 EXISTS this, key
66 Verify that key exists with the tied hash this.
67
68 DELETE this, key
69 Delete the key key from the tied hash this.
70
71 CLEAR this
72 Clear all values from the tied hash this.
73
74 SCALAR this
75 Returns what evaluating the hash in scalar context yields.
76
77 Methods
78 base_hash
79 return or sets the underlying hash for this tie.
80
81 $_[0]->base_hash->{$key}= $value;
82
83 h alias for base_hash.
84
85 $_[0]->h->{$key}= $value;
86
87 private_hash
88 Return or sets the hash of private data associated with this tie.
89 This value will never be touched by any code in this class or its
90 subclasses. It is purely object specific.
91
92 $_[0]->p->{'something'}= 'cake!';
93
94 p alias for private_hash
95
96 method_hash
97 return or sets the hash of methods that are overriden for this tie.
98 Exactly why you would want to use this is a little beyond my
99 imagination, but for those who can think of a reason here is a nice
100 way to do it. :-)
101
102 methods
103 Returns a list of methods that are overriden for this tie. Why this
104 would be useful escapes me, but here it is anyway for Completeness
105 sake, whoever she is, but people are always coding for her so I
106 might as well too.
107
108 Exportable Subs
109 The following subs are exportable on request:
110
111 hash_overload(PAIRS)
112 Returns a reference to a hash tied with the specified callbacks
113 overriden. Just a short cut.
114
115 HASH
116 Constant subroutine equivalent to 0
117
118 METHOD
119 Constant subroutine equivalent to 1
120
121 PRIVATE
122 Constant subroutine equivalent to 2
123
125 Yves Orton, "<yves at cpan.org>"
126
128 Please report any bugs or feature requests to "bug-tie-hash-method at
129 rt.cpan.org", or through the web interface at
130 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tie-Hash-Method>. I
131 will be notified, and then you'll automatically be notified of progress
132 on your bug as I make changes.
133
135 You can find documentation for this module with the perldoc command.
136
137 perldoc Tie::Hash::Method
138
139 You can also look for information at:
140
141 · RT: CPAN's request tracker
142
143 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tie-Hash-Method>
144
145 · AnnoCPAN: Annotated CPAN documentation
146
147 <http://annocpan.org/dist/Tie-Hash-Method>
148
149 · CPAN Ratings
150
151 <http://cpanratings.perl.org/d/Tie-Hash-Method>
152
153 · Search CPAN
154
155 <http://search.cpan.org/dist/Tie-Hash-Method>
156
159 Copyright 2008 Yves Orton, all rights reserved.
160
161 This program is free software; you can redistribute it and/or modify it
162 under the same terms as Perl itself.
163
164
165
166perl v5.28.1 2008-07-24 Tie::Hash::Method(3)