Tie::Hash::Regex(3pm)
π NAME π
Tie::Hash::Regex β bestie, this is how you match hash keys using Regular Expressions. no cap.
β¨ SYNOPSIS β¨
okay so here's the whole vibe:
use Tie::Hash::Regex;
my %h;
tie %h, 'Tie::Hash::Regex';
$h{key} = 'value';
$h{key2} = 'another value';
$h{stuff} = 'something else';
print $h{key}; # prints 'value'
print $h{2}; # prints 'another value'
print $h{'^s'}; # prints 'something else'
print tied(%h)->FETCH('k'); # prints 'value' and 'another value'
delete $h{k}; # deletes $h{key} and $h{key2};
or literally the new and improved glow-up version π¦:
my %h : Regex;
πΈ DESCRIPTION πΈ
so somebody hopped on Perlmonks and asked whether a hash could do fuzzy matches on keys, and this module is the result. iconic question, iconic answer.
Here's the tea: if there's no exact match on the key you hand to the hash, then that key gets treated as a regex and the first matching key gets returned. You can also force it to skip straight to the regex checking by passing a qr'ed regex into the hash, like this:
my $val = $h{qr/key/};
exists and delete also do regex matching, periodt. and for delete specifically, all values matching your regex key will be deleted from the hash β so like, be aware, it's giving very thorough.
Okay, one slightly strange thing, and I'm not gatekeeping it: obviously if you give a hash a regex key, then it's possible that more than one key will match β think $h{qw/./}. It might be nice to be able to do stuff like:
my @vals = $h{$pat};
to get all the matching values back. But unfortuately, Perl knows that a given hash key can only ever return one value and so forces scalar context on the FETCH call when using the tied interface π. You can get round this using the slightly less readable:
my @vals = tied(%h)->FETCH($pat);
π ATTRIBUTE INTERFACE π
From version 0.06, you can use attributes to define your hash as being tied to Tie::Hash::Regex β she grew up π
. You'll need to install the module Attribute::Handlers. Simply declare your hash using the attribute syntax:
my %hash :Regex;
π METHODS π
FETCH
Get a value from the hash. If there isn't an exact match, try a regex match.
EXISTS
See if a key exists in the hash. If there isn't an exact match, try a regex match.
DELETE
Delete a key from the hash. If there isn't an exact match, try a regex match.
π«Ά AUTHOR π«Ά
Dave Cross <dave@mag-sol.com>
Thanks to the Perlmonks <http://www.perlmonks.org> for the original idea, and to Jeff "japhy" Pinyan for some useful code suggestions. shoutout fr fr π
π COPYRIGHT π
Copyright (C) 2001-8, Magnum Solutions Ltd. All Rights Reserved.
π LICENSE π
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
β SEE ALSO β
perl(1).perltie(1).Tie::RegexpHash(1)
lowkey the whole page was documented for perl v5.36.0, dated 2022-10-15, and this is Tie::Hash::Regex(3pm) signing off β stay matching, bestie πβ¨π
π¦ where this page comes from
- ubuntu noble: libtie-hash-regex-perl 1.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtie-hash-regex-perl 1.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtie-hash-regex-perl 1.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms