Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

tie::hash::regex(3pm) [debian man page]

Tie::Hash::Regex(3pm)					User Contributed Perl Documentation				     Tie::Hash::Regex(3pm)

NAME
Tie::Hash::Regex - Match hash keys using Regular Expressions SYNOPSIS
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 (new! improved!) my $h : Regex; DESCRIPTION
Someone asked on Perlmonks if a hash could do fuzzy matches on keys - this is the result. If there's no exact match on the key that you pass to the hash, then the key is treated as a regex and the first matching key is returned. You can force it to leap straight into 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. In the case of "delete" all vlaues matching your regex key will be deleted from the hash. One slightly strange thing. Obviously if you give a hash a regex key, then it's possible that more than one key will match (consider c<$h{qw/./}>). It might be nice to be able to do stuff like: my @vals = $h{$pat}; to get all matching values back. 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. You'll need to install the module Attribute::Handlers. 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. 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) perl v5.10.0 2008-06-30 Tie::Hash::Regex(3pm)

Check Out this Related Man Page

Tie::Memoize(3pm)					 Perl Programmers Reference Guide					 Tie::Memoize(3pm)

NAME
Tie::Memoize - add data to hash when needed SYNOPSIS
require Tie::Memoize; tie %hash, 'Tie::Memoize', &fetch, # The rest is optional $DATA, &exists, {%ini_value}, {%ini_existence}; DESCRIPTION
This package allows a tied hash to autoload its values on the first access, and to use the cached value on the following accesses. Only read-accesses (via fetching the value or "exists") result in calls to the functions; the modify-accesses are performed as on a normal hash. The required arguments during "tie" are the hash, the package, and the reference to the "FETCH"ing function. The optional arguments are an arbitrary scalar $data, the reference to the "EXISTS" function, and initial values of the hash and of the existence cache. Both the "FETCH"ing function and the "EXISTS" functions have the same signature: the arguments are "$key, $data"; $data is the same value as given as argument during tie()ing. Both functions should return an empty list if the value does not exist. If "EXISTS" function is different from the "FETCH"ing function, it should return a TRUE value on success. The "FETCH"ing function should return the intended value if the key is valid. Inheriting from Tie::Memoize The structure of the tied() data is an array reference with elements 0: cache of known values 1: cache of known existence of keys 2: FETCH function 3: EXISTS function 4: $data The rest is for internal usage of this package. In particular, if TIEHASH is overwritten, it should call SUPER::TIEHASH. EXAMPLE
sub slurp { my ($key, $dir) = shift; open my $h, '<', "$dir/$key" or return; local $/; <$h> # slurp it all } sub exists { my ($key, $dir) = shift; return -f "$dir/$key" } tie %hash, 'Tie::Memoize', &slurp, $directory, &exists, { fake_file1 => $content1, fake_file2 => $content2 }, { pretend_does_not_exists => 0, known_to_exist => 1 }; This example treats the slightly modified contents of $directory as a hash. The modifications are that the keys fake_file1 and fake_file2 fetch values $content1 and $content2, and pretend_does_not_exists will never be accessed. Additionally, the existence of known_to_exist is never checked (so if it does not exists when its content is needed, the user of %hash may be confused). BUGS
FIRSTKEY and NEXTKEY methods go through the keys which were already read, not all the possible keys of the hash. AUTHOR
Ilya Zakharevich <mailto:perl-module-hash-memoize@ilyaz.org>. perl v5.18.2 2013-11-04 Tie::Memoize(3pm)
Man Page