Debian Codegraph @

lib/Tie/Hash/Regex.pm

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1
2 =head1 NAME
3
4 Tie::Hash::Regex - Match hash keys using Regular Expressions
5
6 =head1 SYNOPSIS
7
8 use Tie::Hash::Regex;
9 my %h;
10
11 tie %h, 'Tie::Hash::Regex';
12
13 $h{key} = 'value';
14 $h{key2} = 'another value';
15 $h{stuff} = 'something else';
16
17 print $h{key}; # prints 'value'
18 print $h{2}; # prints 'another value'
19 print $h{'^s'}; # prints 'something else'
20
21 print tied(%h)->FETCH('k'); # prints 'value' and 'another value'
22
23 delete $h{k}; # deletes $h{key} and $h{key2};
24
25 or (new! improved!)
26
27 my %h : Regex;
28
29 =head1 DESCRIPTION
30
31 Someone asked on Perlmonks if a hash could do fuzzy matches on keys - this
32 is the result.
33
34 If there's no exact match on the key that you pass to the hash, then the
35 key is treated as a regex and the first matching key is returned. You can
36 force it to leap straight into the regex checking by passing a qr'ed
37 regex into the hash like this:
38
39 my $val = $h{qr/key/};
40
41 C<exists> and C<delete> also do regex matching. In the case of C<delete>
42 I<all> values matching your regex key will be deleted from the hash.
43
44 One slightly strange thing. Obviously if you give a hash a regex key, then
45 it's possible that more than one key will match (consider C<$h{qw/./}>).
46 It might be nice to be able to do stuff like:
47
48 my @vals = $h{$pat};
49
50 to get I<all> matching values back. Unfortuately, Perl knows that a given
51 hash key can only ever return one value and so forces scalar context on
52 the C<FETCH> call when using the tied interface. You can get round this
53 using the slightly less readable:
54
55 my @vals = tied(%h)->FETCH($pat);
56
57 =head2 ATTRIBUTE INTERFACE
58
59 From version 0.06, you can use attributes to define your hash as being
60 tied to Tie::Hash::Regex. You'll need to install the module
61 Attribute::Handlers. Simply declare your hash using the attribute
62 syntax:
63
64 my %hash :Regex;
65
66
67 =cut
68
69 package Tie::Hash::Regex;
70
71 use 5.006;
72 use strict;
73 use warnings;
74 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
75
76 require Exporter;
77 require Tie::Hash;
78 use Attribute::Handlers autotie => { "__CALLER__::Regex" => __PACKAGE__ };
79
80 @ISA = qw(Exporter Tie::StdHash);
81 @EXPORT = qw();
82 @EXPORT_OK =();
83
84 $VERSION = 1.14;
85
86 =head1 METHODS
87
88 =head2 FETCH
89
90 Get a value from the hash. If there isn't an exact match try a regex
91 match.
92
93 =cut
94
95 sub FETCH {
96 my $self = shift;
97 my $key = shift;
98
99 my $is_re = (ref $key eq 'Regexp');
100
101 return $self->{$key} if !$is_re && exists $self->{$key};
102
103 $key = qr/$key/ unless $is_re;
104
105 # NOTE: wantarray will _never_ be true when FETCH is called
106 # using the standard hash semantics. I've put that piece
107 # of code in for people who are happy using syntax like:
108 # tied(%h)->FETCH(qr/$pat/);
109 if (wantarray) {
110 return @{$self}{ grep /$key/, keys %$self };
111 } else {
112 /$key/ and return $self->{$_} for keys %$self;
113 }
114
115 return;
116 }
117
118 =head2 EXISTS
119
120 See if a key exists in the hash. If there isn't an exact match try a regex
121 match.
122
123 =cut
124
125 sub EXISTS {
126 my $self = shift;
127 my $key = shift;
128
129 my $is_re = (ref $key eq 'Regexp');
130
131 return 1 if !$is_re && exists $self->{$key};
132
133 $key = qr/$key/ unless $is_re;
134
135 /$key/ && return 1 for keys %$self;
136
137 return;
138 }
139
140 =head2 DELETE
141
142 Delete a key from the hash. If there isn't an exact match try a regex
143 match.
144
145 =cut
146
147 sub DELETE {
148 my $self = shift;
149 my $key = shift;
150
151 my $is_re = (ref $key eq 'Regexp');
152
153 return delete $self->{$key} if !$is_re && exists $self->{$key};
154
155 $key = qr/$key/ unless $is_re;
156
157 for (keys %$self) {
158 if (/$key/) {
159 delete $self->{$_};
160 }
161 }
162 }
163
164 1;
165 __END__
166
167
168 =head1 AUTHOR
169
170 Dave Cross <[email protected]>
171
172 Thanks to the Perlmonks L<http://www.perlmonks.org> for the original idea
173 and to Jeff "japhy" Pinyan for some useful code suggestions.
174
175 =head1 COPYRIGHT
176
177 Copyright (C) 2001-8, Magnum Solutions Ltd. All Rights Reserved.
178
179 =head1 LICENSE
180
181 This script is free software; you can redistribute it and/or
182 modify it under the same terms as Perl itself.
183
184 =head1 SEE ALSO
185
186 perl(1).
187
188 perltie(1).
189
190 Tie::RegexpHash(1)
191
192 =cut