| 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 = _compile($key) unless $is_re; |
| 104 |
return unless defined $key; |
| 105 |
|
| 106 |
# NOTE: wantarray will _never_ be true when FETCH is called |
| 107 |
# using the standard hash semantics. I've put that piece |
| 108 |
# of code in for people who are happy using syntax like: |
| 109 |
# tied(%h)->FETCH(qr/$pat/); |
| 110 |
if (wantarray) { |
| 111 |
return @{$self}{ grep /$key/, keys %$self }; |
| 112 |
} else { |
| 113 |
/$key/ and return $self->{$_} for keys %$self; |
| 114 |
} |
| 115 |
|
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
=head2 EXISTS |
| 120 |
|
| 121 |
See if a key exists in the hash. If there isn't an exact match try a regex |
| 122 |
match. |
| 123 |
|
| 124 |
=cut |
| 125 |
|
| 126 |
sub EXISTS { |
| 127 |
my $self = shift; |
| 128 |
my $key = shift; |
| 129 |
|
| 130 |
my $is_re = (ref $key eq 'Regexp'); |
| 131 |
|
| 132 |
return 1 if !$is_re && exists $self->{$key}; |
| 133 |
|
| •134 |
$key = _compile($key) unless $is_re; |
| 135 |
return unless defined $key; |
| 136 |
|
| 137 |
/$key/ && return 1 for keys %$self; |
| 138 |
|
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
=head2 DELETE |
| 143 |
|
| 144 |
Delete a key from the hash. If there isn't an exact match try a regex |
| 145 |
match. |
| 146 |
|
| 147 |
=cut |
| 148 |
|
| 149 |
sub DELETE { |
| 150 |
my $self = shift; |
| 151 |
my $key = shift; |
| 152 |
|
| 153 |
my $is_re = (ref $key eq 'Regexp'); |
| 154 |
|
| 155 |
return delete $self->{$key} if !$is_re && exists $self->{$key}; |
| 156 |
|
| •157 |
$key = _compile($key) unless $is_re; |
| 158 |
return unless defined $key; |
| 159 |
|
| 160 |
for (keys %$self) { |
| 161 |
if (/$key/) { |
| 162 |
delete $self->{$_}; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| •167 |
sub _compile { |
| 168 |
my ($key) = @_; |
| 169 |
|
| 170 |
my $re = eval { qr/$key/ }; |
| 171 |
|
| 172 |
# This will be undef if the "eval" failed |
| 173 |
return $re; |
| 174 |
} |
| 175 |
|
| 176 |
1; |
| 177 |
__END__ |
| 178 |
|
| 179 |
|
| 180 |
=head1 AUTHOR |
| 181 |
|
| 182 |
Dave Cross <[email protected]> |
| 183 |
|
| 184 |
Thanks to the Perlmonks L<http://www.perlmonks.org> for the original idea |
| 185 |
and to Jeff "japhy" Pinyan for some useful code suggestions. |
| 186 |
|
| 187 |
=head1 COPYRIGHT |
| 188 |
|
| 189 |
Copyright (C) 2001-8, Magnum Solutions Ltd. All Rights Reserved. |
| 190 |
|
| 191 |
=head1 LICENSE |
| 192 |
|
| 193 |
This script is free software; you can redistribute it and/or |
| 194 |
modify it under the same terms as Perl itself. |
| 195 |
|
| 196 |
=head1 SEE ALSO |
| 197 |
|
| 198 |
perl(1). |
| 199 |
|
| 200 |
perltie(1). |
| 201 |
|
| 202 |
Tie::RegexpHash(1) |
| 203 |
|
| 204 |
=cut |