Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

( #19608=categorized question: print w/ replies, xml ) Need Help??
Contributed by draco_iii on Jun 23, 2000 at 19:53 UTC
Q&A  > subroutines


Description:

I have a routine that can return different information depending on what kind of varible is asked of it.
If asking for a $ it returns a single set of data, if you ask for an @ it will return the array. But I can also have it return the information in a hash format. I've thought about passing the varible as a reference thinking that this would be a better way to determine it.

Is this possible?

Answer: How do you test for return type($,@,%)?
contributed by davorg

A subroutine can only return a scalar or a list. What the caller does with the result is completely out of the subroutine's control.

For example:
sub array { return 0 .. 5; } my %hash = array();

However, you _could_ do something like the code below, but it's pretty strange stuff and I'm not sure how useful it is :-)

#!/usr/bin/perl -w use strict; sub get_stuff { my $ref = shift; if (ref $ref) { if (ref $ref eq 'SCALAR') { $$ref = 'This is a scalar'; } elsif (ref $ref eq 'ARRAY') { @$ref = qw(This is an array); } elsif (ref $ref eq 'HASH' ) { %$ref = (1 => 'This', 2 => 'is', 3 => 'a', 4 => 'Hash'); } else { die "Invalid reference type passed to get_stuff.\n"; } } else { die "Non-reference passed to get_stuff.\n"; } } my ($scalar, @array, %hash); get_stuff(\$scalar); get_stuff(\@array); get_stuff(\%hash); print "$scalar\n"; $"='|'; print "@array\n"; print map { $hash{$_} . ' ' } sort keys %hash;
Answer: How do you test for return type($,@,%)?
contributed by QandAEditors

A subroutine may only know whether its caller wants a SCALAR or an ARRAY. The wantarray function handles this.

sub test { if (wantarray){ return 'One String'; } else { return ('A', 'List', 'of', 'Strings'); } }
There is no built-in way to tell if a subroutine's caller wants a hash, but davorg has posted a sample of how you would do this (see his answer).

The DBI module uses a similar technique (non-relevant code removed for brevity):

sub fetchall_arrayref { my $slice= shift || []; my $mode = ref $slice; if ($mode eq 'ARRAY') { push @rows, [ @$row ] while($row = $sth->fetch); } elsif ($mode eq 'HASH') { push @rows, $row while ($row = $sth->fetchrow_hashref); } else { Carp::croak("fetchall_arrayref($mode) invalid") } return \@rows; }
If you call fetchall_arrayref with an array reference (or no reference), it will return array references. If you call it with a hash reference, it will return hash references.
$sth->fetchall_arrayref( [] ) # returns Array references $sth->fetchall_arrayref( {} ) # returns Hash references

Cool stuff!

Russ

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2015-07-05 21:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (68 votes), past polls