http://www.perlmonks.org?node_id=704945

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

Replies are listed 'Best First'.
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 (Chancellor) 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 :-))