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 #---------------------------------------------------------------------
23 use vars qw(@ISA $VERSION);
27 #=====================================================================
28 # Package Global Variables:
32 #=====================================================================
34 #---------------------------------------------------------------------
36 # The method invoked by the command `tie %hash, classname'.
37 # Associates a new hash instance with the specified class.
44 #---------------------------------------------------------------------
45 # STORE this, key, value
46 # Store datum *value* into *key* for the tied hash *this*.
50 $_[0]->{lc $_[1]} = [ $_[1], $_[2] ];
53 #---------------------------------------------------------------------
55 # Retrieve the datum in *key* for the tied hash *this*.
59 my $v = $_[0]->{lc $_[1]};
60 ($v ? $v->[1] : undef);
63 #---------------------------------------------------------------------
65 # Return the (key, value) pair for the first key in the hash.
69 my $a = scalar keys %{$_[0]};
73 #---------------------------------------------------------------------
74 # NEXTKEY this, lastkey
75 # Return the next (key, value) pair for the hash.
79 my $v = (each %{$_[0]})[1];
80 ($v ? $v->[0] : undef );
83 #---------------------------------------------------------------------
85 # Return bucket usage information for the hash (0 if empty).
92 #---------------------------------------------------------------------
94 # Verify that *key* exists with the tied hash *this*.
98 exists $_[0]->{lc $_[1]};
101 #---------------------------------------------------------------------
103 # Delete the key *key* from the tied hash *this*.
104 # Returns the old value, or undef if it didn't exist.
108 my $v = delete $_[0]->{lc $_[1]};
109 ($v ? $v->[1] : undef);
112 #---------------------------------------------------------------------
114 # Clear all values from the tied hash *this*.
121 #=====================================================================
123 #---------------------------------------------------------------------
124 # Return the case of KEY.
128 my $v = $_[0]->{lc $_[1]};
129 ($v ? $v->[0] : undef);
132 #=====================================================================
133 # Package Return Value:
141 Tie::CPHash - Case preserving but case insensitive hash table
146 tie %cphash, 'Tie::CPHash';
148 $cphash{'Hello World'} = 'Hi there!';
149 printf("The key `%s' was used to store `%s'.\n",
150 tied(%cphash)->key('HELLO WORLD'),
151 $cphash{'HELLO world'});
155 The B<Tie::CPHash> module provides a hash table that is case
156 preserving but case insensitive. This means that
158 $cphash{KEY} $cphash{key}
159 $cphash{Key} $cphash{keY}
161 all refer to the same entry. Also, the hash remembers which form of
162 the key was last used to store the entry. The C<keys> and C<each>
163 functions will return the key that was used to set the value.
165 An example should make this clear:
167 tie %h, 'Tie::CPHash';
169 print $h{HELLO}; # Prints 'World'
170 print keys(%h); # Prints 'Hello'
172 print $h{hello}; # Prints 'WORLD'
173 print keys(%h); # Prints 'HELLO'
175 The additional C<key> method lets you fetch the case of a specific key:
177 # When run after the previous example, this prints 'HELLO':
178 print tied(%h)->key('Hello');
180 (The C<tied> function returns the object that C<%h> is tied to.)
182 If you need a case insensitive hash, but don't need to preserve case,
183 just use C<$hash{lc $key}> instead of C<$hash{$key}>. This has a lot
184 less overhead than B<Tie::CPHash>.
188 Christopher J. Madsen E<lt>F<cjm@pobox.com>E<gt>
193 # tmtrack-file-task: "Tie::CPHash.pm"