| 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 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 3 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 77 | 3 | require Tie::Hash; | |||||||||||||||||||||||||||
| 78 | 3 | ▶ | use Attribute::Handlers autotie => { "__CALLER__::Regex" => __PACKAGE__ }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | @ISA = qw(Exporter Tie::StdHash); | |||||||||||||||||||||||||||
| 81 | 3 | @EXPORT = qw(); | |||||||||||||||||||||||||||
| 82 | 3 | @EXPORT_OK =(); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | $VERSION = '2.0.0'; | |||||||||||||||||||||||||||
| 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 | 17 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 96 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 97 | 17 | my $key = shift; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 17 | my $is_re = (ref $key eq 'Regexp'); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 17 | ▶ | return $self->{$key} if !$is_re && exists $self->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $is_re and exists $self->{$key}
MC/DC100%2/2
not $is_re and exists $self->{$key}
not $is_re
exists $self->{$key}
Truth table100%3/3
not(A) and B
A= not $is_reB= exists $self->{$key}
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 15 | ▶ | $key = _compile($key) unless $is_re; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 15 | ▶ | return unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 7 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | return @{$self}{ grep /$key/, keys %$self }; | |||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||
| 113 | 6 | ▶ | /$key/ and return $self->{$_} for keys %$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | 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 | 12 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 127 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 128 | 12 | my $key = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 12 | my $is_re = (ref $key eq 'Regexp'); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 12 | ▶ | return 1 if !$is_re && exists $self->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $is_re and exists $self->{$key}
MC/DC100%2/2
not $is_re and exists $self->{$key}
not $is_re
exists $self->{$key}
Truth table100%3/3
not(A) and B
A= not $is_reB= exists $self->{$key}
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | ▶ | $key = _compile($key) unless $is_re; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 11 | ▶ | return unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 3 | ▶ | /$key/ && return 1 for keys %$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | 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 | 11 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 150 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 151 | 11 | my $key = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 11 | my $is_re = (ref $key eq 'Regexp'); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 11 | ▶ | return delete $self->{$key} if !$is_re && exists $self->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $is_re and exists $self->{$key}
MC/DC100%2/2
not $is_re and exists $self->{$key}
not $is_re
exists $self->{$key}
Truth table100%3/3
not(A) and B
A= not $is_reB= exists $self->{$key}
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 10 | ▶ | $key = _compile($key) unless $is_re; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 10 | ▶ | return unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2 | for (keys %$self) { | |||||||||||||||||||||||||||
| 161 | 3 | ▶ | if (/$key/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 2 | delete $self->{$_}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 33 | ▶ | sub _compile { | ||||||||||||||||||||||||||
|
Subroutine
_compile: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 33 | my ($key) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 33 | my $re = eval { qr/$key/ }; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # This will be undef if the "eval" failed | ||||||||||||||||||||||||||||
| 173 | 33 | return $re; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 3 | 1; | |||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||