A less approximate version of Tie::Hash::Approx

Adam Bernard ab11+londonpm at sanger.ac.uk
Thu Mar 13 09:23:32 GMT 2008


On 13/03/2008, Zbigniew Lukasiak <zzbbyy at gmail.com> wrote:
> On Wed, Mar 12, 2008 at 3:50 PM, David Cantrell <david at cantrell.org.uk> wrote:
>  > I need a module that will let me tie a hash so that if I try to FETCH a
>  >  value for a key that exists, then it'll return the appropriate value,
>  >  but will also let me fetch values for keys that match certain rules.
>  >
>  >  eg so that ...
>  >
>  >     tie %hash, ....
>  >     $hash{Hobbes} = 'feline';
>  >     $hash{Lassie} = 'canine';
>  >     # set up some magic here
>  >
>  >     print "Hobbes is a $hash{Hobbes}\n";
>  >     print "Lassie is a $hash{Lassie}\n";
>  >     print "42 is an $hash{42}\n";        # 42 gets caught by some magic
>  >     print "3.5 is a $hash{3.5}\n";       # 3.5 matches some other magic
>  >     print "Skippy is a $hash{Skippy}\n"; # Skippy doesn't exist
>  >
>  >  will print:
>  >
>  >     Hobbes is a feline
>  >     Lassie is a canine
>  >     42 is an integer
>  >     3.5 is a floating point number
>  >     Skippy is a Use of uninitialized value in concatenation
>  >
>  >  without me having to define values for all real numbers, which would be
>  >  the sort of task I might set a work-experience kid if I don't like their
>  >  haircut.
>  >
>  >  Can anyone point me at something on the CPAN, or am I going to have to
>  >  write this myself?
>  >
>
>
> Maybe not very helpful - but I think it is hard to see where you would
>  like to stop your generalisation before getting a universal function
>  call instead of a hash lookup.

I know of no module that does this already, but perhaps one could do
it by using regexes and/or code blocks as hash keys - if a searched
key wasn't present among the normal values (possibly including the
stringified forms of references), you could fall back to checking it
against those expressions?

Adam


More information about the london.pm mailing list