Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Determining the type of a subroutine parameter

by esr (Scribe)
on Aug 18, 2008 at 14:48 UTC ( #704945=perlquestion: print w/ replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

I would like to call a subroutine with one of the parameters being either a scalar variable or an array reference. If I were to do that, is there a way in the subroutine to tell which has been specified?

Comment on Determining the type of a subroutine parameter
Re: Determining the type of a subroutine parameter
by moritz (Cardinal) on Aug 18, 2008 at 14:51 UTC
Re: Determining the type of a subroutine parameter
by FunkyMonk (Canon) on Aug 18, 2008 at 14:51 UTC
    Have a look at ref
    my ( $scalar, @array ); foo( \$scalar ); foo( \@array ); sub foo { print ref $_[0], "\n"; } __END__ SCALAR ARRAY


    Unless I state otherwise, all my code runs with strict and warnings
Re: Determining the type of a subroutine parameter
by Bloodnok (Vicar) on Aug 18, 2008 at 15:11 UTC
    I don't think that quite answers the OP i.e. from the POV of a scalar variable - to use the given example ...

    sub foo { print "Type: ", ref $_[0], "\n"; } my ( $scalar, @array ); foo( $scalar ); foo( \$scalar ); foo( \@array ); __END__ Type: Type: SCALAR Type: ARRAY
    This to determine if a scalar has been passed, calling ref on it will return the empty string.

    HTH ,

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://704945]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-07-06 19: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 (81 votes), past polls