Mur has asked for the wisdom of the Perl Monks concerning the following question:

I have an application which requires "near match" key lookups using English words. It makes use of the Lingua::Stem module to do things like this:
my %hash; tie %hash, 'Nexcerpt::Keyword'; $hash{dog} = 1; print $hash{dogs}; # should print '1' print $hash{dogged}; # ditto
My approach was to write your basic Tie::Hash class, and to override EXISTS, STORE, etc. One thing I tried to do but which doesn't appear to work is to change the way exists() works: it should return '0' if no match is found, or the list of hash keys that match (or the first match, if not in list context).

However, what appears to be happening is that my nice list of values returned at the end of my EXISTS routine, is being silently converted somewhere internally to a true/false. Is there a way around this? (I could always just write another method called "matches", but I'm stubborn.)
--
Jeff Boes
Database Engineer
Nexcerpt, Inc.
vox 269.226.9550 ext 24
fax 269.349.9076
 http://www.nexcerpt.com
...Nexcerpt...Connecting People With Expertise

Replies are listed 'Best First'.
Re: Override exists()?
by Joost (Canon) on Dec 13, 2002 at 14:50 UTC
    exists should not return the value for the specified key; it should always return true if there is such a KEY (the value may be undefined or false).

    Why don't you just return a true value for EXISTS and a list (or array-ref) of matches for FETCH?

    J.

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      I tried that, too, in the time since I posted:
      my @x = $hash{$key};
      But inside the code, wantarray is not set. I suppose I could just return a listref, but ... I'm stubborn.
      --
      Jeff Boes
      Database Engineer
      Nexcerpt, Inc.
      vox 269.226.9550 ext 24
      fax 269.349.9076
       http://www.nexcerpt.com
      ...Nexcerpt...Connecting People With Expertise
(tye)Re: Override exists()?
by tye (Sage) on Dec 13, 2002 at 18:14 UTC

    When you tie data structures in Perl, you can't radically change the interface. That is, hash values still have to be scalars, hash keys still have to be scalars, etc. To change this you'll have to patch the Perl source code and get the patch accepted.

            - tye