1 #---------------------------------------------------------------------
4 # Copyright 1997 Christopher J. Madsen
6 # Author: Christopher J. Madsen <chris_madsen@geocities.com>
8 # Version: 1.001 (25-Oct-1998)
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 # Convert RCS revision number to d.ddd format:
33 $VERSION = sprintf('%d.%03d', '1.001 ' =~ /(\d+)\.(\d+)/);
36 #=====================================================================
38 #---------------------------------------------------------------------
40 # The method invoked by the command `tie %hash, classname'.
41 # Associates a new hash instance with the specified class.
48 #---------------------------------------------------------------------
49 # STORE this, key, value
50 # Store datum *value* into *key* for the tied hash *this*.
54 $_[0]->{lc $_[1]} = [ $_[1], $_[2] ];
57 #---------------------------------------------------------------------
59 # Retrieve the datum in *key* for the tied hash *this*.
63 my $v = $_[0]->{lc $_[1]};
64 ($v ? $v->[1] : undef);
67 #---------------------------------------------------------------------
69 # Return the (key, value) pair for the first key in the hash.
73 my $a = scalar keys %{$_[0]};
77 #---------------------------------------------------------------------
78 # NEXTKEY this, lastkey
79 # Return the next (key, value) pair for the hash.
83 my $v = (each %{$_[0]})[1];
84 ($v ? $v->[0] : undef );
87 #---------------------------------------------------------------------
89 # Verify that *key* exists with the tied hash *this*.
93 exists $_[0]->{lc $_[1]};
96 #---------------------------------------------------------------------
98 # Delete the key *key* from the tied hash *this*.
99 # Returns the old value, or undef if it didn't exist.
103 my $v = delete $_[0]->{lc $_[1]};
104 ($v ? $v->[1] : undef);
107 #---------------------------------------------------------------------
109 # Clear all values from the tied hash *this*.
116 #=====================================================================
118 #---------------------------------------------------------------------
119 # Return the case of KEY.
123 my $v = $_[0]->{lc $_[1]};
124 ($v ? $v->[0] : undef);
127 #=====================================================================
128 # Package Return Value:
136 Tie::CPHash - Case preserving but case insensitive hash table
141 tie %cphash, 'Tie::CPHash';
143 $cphash{'Hello World'} = 'Hi there!';
144 printf("The key `%s' was used to store `%s'.\n",
145 tied(%cphash)->key('HELLO WORLD'),
146 $cphash{'HELLO world'});
150 The B<Tie::CPHash> provides a hash table that is case preserving but
151 case insensitive. This means that
153 $cphash{KEY} $cphash{key}
154 $cphash{Key} $cphash{keY}
156 all refer to the same entry. Also, the hash remembers which form of
157 the key was last used to store the entry. The C<keys> and C<each>
158 functions will return the key that was used to set the value.
160 An example should make this clear:
162 tie %h, 'Tie::CPHash';
164 print $h{HELLO}; # Prints 'World'
165 print keys(%h); # Prints 'Hello'
167 print $h{hello}; # Prints 'WORLD'
168 print keys(%h); # Prints 'HELLO'
170 The additional C<key> method lets you fetch the case of a specific key:
172 # When run after the previous example, this prints 'HELLO':
173 print tied(%h)->key('Hello');
175 (The C<tied> function returns the object that C<%h> is tied to.)
177 If you need a case insensitive hash, but don't need to preserve case,
178 just use C<$hash{lc $key}> instead of C<$hash{$key}>. This has a lot
179 less overhead than B<Tie::CPHash>.
183 Christopher J. Madsen E<lt>F<chris_madsen@geocities.com>E<gt>
188 # tmtrack-file-task: "Tie::CPHash.pm"