in reply to Re: Re: wantarray and Tied Hashes
in thread wantarray and Tied Hashes

People would be a bit surprised to find $hash{key} returning a list. I like the fact that not even tied hashes can break this expectation. I don't think you should use $hash{key} as an all-purpose replacement for funct("key"). If you are going to provide your functionality via a "hash-like" interface, then I think you should make it act like a hash; which means that it can only take a single string as input and can only return a single value.

Next you'll be wanting to tie scalars such that $x returns a list!!

If you want to return multiple values, then you'll have to return a reference to an array.

What I did with this was that I allowed the user to specify whether they wanted a list (er, ref to array) for each hash:

tied(%hash)->WantList("always"); tied(%hash)->WantList("never"); tied(%hash)->WantList("forMultiples");
(that isn't really a suggested interface, just a quick hack at a way to describe what I'm talking about). So each user could decide whether they wanted to write code like:
$first= $hash{key}[0]; # or $first= $hash{key}; # or $values= $hash{key}; $first= ref($values) ? $values->[0] : $values;

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: wantarray and Tied Hashes
by Vynce (Friar) on May 26, 2001 at 03:04 UTC

    i agree, at least until perl6. $hash{key} says "scalar" right on it at the beginning. if you could make perl agree to do it, i wouldn't mind @hash{key} returning a list, but i suspect that would cause other problems.

    and, as i understand it, in perl6 you'll say %hash{key} instead, anyway.