in reply to How do you test for return type($,@,%)?

First, a little background info. There are two major contexts we are concerned with here: scalar and list (the others are boolean, void and interpolative -- but that's another topic). There is no hash context. wantarray will tell you if the context is scalar or list, but not if you want a hash.

That said, you can only tell if you want one value or a list of values. For example:
$var = mysub(); @var = mysub(); %var = mysub(); sub mysub { print "You want a scalar\n" unless wantarray; print "You either want a hash or an array\n" if wantarray; }
One quick way I can think of to get around this is by passing references. For example,
my($var,@var,%var); &mysub(\$var); &mysub(\@var); &mysub(\%var); sub mysub { my($ref) = shift; print "You want a scalar\n" if ref($ref) eq 'SCALAR'; print "You want an array\n" if ref($ref) eq 'ARRAY'; print "You want a hash\n" if ref($ref) eq 'HASH'; }
Hope that helps,
Shendal

Replies are listed 'Best First'.
RE: RE: Answer: How do you test for return type($,@,%)?
by chromatic (Archbishop) on Jun 24, 2000 at 02:11 UTC
    I think this is an XYZ question. The interface design question is backwards.

    Instead of asking, "What kind of information does this subroutine return?" he's asking "What kind of information does the caller want?"

    The only place where this question might fit is when you're replacing an existing subroutine, and the interface already takes context into account. (Pointing out wantarray takes care of that.)

    In short, I'm terribly confused by this question altogether, and very curious as to what lead to it.