1 #---------------------------------------------------------------------
4 # Copyright 1997 Christopher J. Madsen
6 # Author: Christopher J. Madsen <cjm@pobox.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the same terms as Perl itself.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
16 # GNU General Public License or the Artistic License for more details.
18 # Case preserving but case insensitive hash
19 #---------------------------------------------------------------------
24 use vars qw(@ISA $VERSION);
28 #=====================================================================
29 # Package Global Variables:
33 #=====================================================================
35 #---------------------------------------------------------------------
37 # The method invoked by the command `tie %hash, classname'.
38 # Associates a new hash instance with the specified class.
45 #---------------------------------------------------------------------
46 # STORE this, key, value
47 # Store datum *value* into *key* for the tied hash *this*.
51 $_[0]->{lc $_[1]} = [ $_[1], $_[2] ];
54 #---------------------------------------------------------------------
56 # Retrieve the datum in *key* for the tied hash *this*.
60 my $v = $_[0]->{lc $_[1]};
61 ($v ? $v->[1] : undef);
64 #---------------------------------------------------------------------
66 # Return the (key, value) pair for the first key in the hash.
70 my $a = scalar keys %{$_[0]};
74 #---------------------------------------------------------------------
75 # NEXTKEY this, lastkey
76 # Return the next (key, value) pair for the hash.
80 my $v = (each %{$_[0]})[1];
81 ($v ? $v->[0] : undef );
84 #---------------------------------------------------------------------
86 # Return bucket usage information for the hash (0 if empty).
93 #---------------------------------------------------------------------
95 # Verify that *key* exists with the tied hash *this*.
99 exists $_[0]->{lc $_[1]};
102 #---------------------------------------------------------------------
104 # Delete the key *key* from the tied hash *this*.
105 # Returns the old value, or undef if it didn't exist.
109 my $v = delete $_[0]->{lc $_[1]};
110 ($v ? $v->[1] : undef);
113 #---------------------------------------------------------------------
115 # Clear all values from the tied hash *this*.
122 #=====================================================================
124 #---------------------------------------------------------------------
125 # Return the case of KEY.
129 my $v = $_[0]->{lc $_[1]};
130 ($v ? $v->[0] : undef);
133 #=====================================================================
134 # Package Return Value:
142 Tie::CPHash - Case preserving but case insensitive hash table
147 tie %cphash, 'Tie::CPHash';
149 $cphash{'Hello World'} = 'Hi there!';
150 printf("The key `%s' was used to store `%s'.\n",
151 tied(%cphash)->key('HELLO WORLD'),
152 $cphash{'HELLO world'});
156 The B<Tie::CPHash> module provides a hash table that is case
157 preserving but case insensitive. This means that
159 $cphash{KEY} $cphash{key}
160 $cphash{Key} $cphash{keY}
162 all refer to the same entry. Also, the hash remembers which form of
163 the key was last used to store the entry. The C<keys> and C<each>
164 functions will return the key that was used to set the value.
166 An example should make this clear:
168 tie %h, 'Tie::CPHash';
170 print $h{HELLO}; # Prints 'World'
171 print keys(%h); # Prints 'Hello'
173 print $h{hello}; # Prints 'WORLD'
174 print keys(%h); # Prints 'HELLO'
176 The additional C<key> method lets you fetch the case of a specific key:
178 # When run after the previous example, this prints 'HELLO':
179 print tied(%h)->key('Hello');
181 (The C<tied> function returns the object that C<%h> is tied to.)
183 If you need a case insensitive hash, but don't need to preserve case,
184 just use C<$hash{lc $key}> instead of C<$hash{$key}>. This has a lot
185 less overhead than B<Tie::CPHash>.
189 Christopher J. Madsen E<lt>F<cjm@pobox.com>E<gt>
194 # tmtrack-file-task: "Tie::CPHash.pm"