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

Thanks for the ideas. I tried adding the following lines to my test script:

$scalar = tied(%h)->FETCH('one'); @array = tied(%h)->FETCH('two');

And that returned

wantarray is false wantarray is true

so it seems to me that Perl is doing something to force scalar context somewhere in the tie interface.

To explain why I want FETCH to be callable in a list context, look at Tie::Hash::Regex. I'm thinking that if a hash is matching keys using regexes, then you could match more than one key and return a list of values in list context.

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
(tye)Re: wantarray and Tied Hashes
by tye (Sage) on May 24, 2001 at 23:31 UTC

    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")

      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.