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

Is there a way to determine what the caller is wanting to return other than 'wantarray'? Can I determine if the list the caller wants is a hash?
use strict; my $txt=callMe(1,2,3); print "txt=$txt\n"; my @tmp=callMe(1,2,3); print "tmp=@tmp\n"; my %list=callMe(1,2,3); foreach my $key (keys(%list)){print "list{$key}=$list{$key}\n";} sub callMe{ my @in=@_; if(wantarray){ #how do I determine if the caller wants an array or a hash? return reverse(@in); } else{return join(",",reverse(@in));} }
--------------------
txt=3,2,1
tmp=3 2 1
list{1}=
list{3}=2

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com

Replies are listed 'Best First'.
Re: wantarray like wanthash
by dragonchild (Archbishop) on Dec 12, 2005 at 21:03 UTC
    "wantarray" is really a misnomer. It should be called "wantlist" because what it determines is if the calling context is list, scalar, or void.

    You're looking for Contextual::Return.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: wantarray like wanthash
by Roy Johnson (Monsignor) on Dec 12, 2005 at 21:02 UTC
    Nothing built-in, because it's really just telling you whether the sub was called in scalar, list, or void context.

    Look at Want for a module that gives you more contexts.


    Caution: Contents may have been coded under pressure.
Re: wantarray like wanthash
by Fletch (Bishop) on Dec 12, 2005 at 21:03 UTC

    If you do some searching you'll find discussion on how wantarray would have been more properly named wantlist. There's three contexts: scalar, list, and void. Assigning to a hash is just using the fact that a hash can be initialized from a list of key-value pairs (so no, you couldn't tell the difference).

Re: wantarray like wanthash
by GrandFather (Saint) on Dec 12, 2005 at 21:15 UTC

    wantarray should perhps be called something like callcontext. For this purpose Perl has three contexts: void, scalar and list, and wantarray is undef, false and true.

    Note in general that in list context a hash turns into an array in any case - think for example what happens when you pass a hash into a sub. To that extent hashes and arrays are interchangeable.

    Consider this:

    use warnings; use strict; use Data::Dumper; my %hash = (first => '1', second => '2'); print "Original: " . Dumper (\%hash); my %newHash = %{inAndOut (%hash)}; print "\nnew hash: " . Dumper (\%newHash); sub inAndOut { my (%hash) = @_; print "\nin sub: " . Dumper (\%hash); return {%hash}; }

    prints:

    Original: $VAR1 = { 'first' => '1', 'second' => '2' }; in sub: $VAR1 = { 'first' => '1', 'second' => '2' }; new hash: $VAR1 = { 'first' => '1', 'second' => '2' };

    DWIM is Perl's answer to Gödel
Re: wantarray like wanthash
by jZed (Prior) on Dec 12, 2005 at 21:34 UTC
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = try_it(); my $scalar = try_it(); print Dumper \%hash; print "\n$scalar\n"; sub try_it { return (a=>1,b=>2) if wantarray; return "foo"; }