in reply to Re: Re: What is inside of () on a sub call?
in thread What is inside of () on a sub call?

It will only accept a single scalar. That scalar might be a constant string, a scalar variable, or a reference to anything. Perl prototypes are not for enforcing types or informing anybody of expected content. That is a major difference with C/C++ or Java.

Characterization only happens if your type is listed in the prototype as \$. In that case a reference to an lvalue scalar is admitted. References to hashes, arrays, code etc. are all possible.

Update: duff++, good catch.

After Compline,
Zaxo

  • Comment on Re: Re: Re: What is inside of () on a sub call?

Replies are listed 'Best First'.
Re: Re: Re: Re: What is inside of () on a sub call?
by duff (Parson) on May 22, 2004 at 15:31 UTC
    It will only accept a single scalar.
    Not quite. It accepts a single argument and evaluates it in scalar context. Thus a subroutine prototyped with ($) will accept an array as an argument (and since it's in scalar context, you dutifully get the number of elements in the array in $_[0]). Examples:
    sub foo ($$) { ... } foo($a); # invalid, not enough args foo(@a); # invalid, not enough args foo($a,$b); # valid foo(@a,@b); # valid foo($a,@b); # valid foo(@a,$b); # valid $_[0] gets the number of elements in @a
    That last one tends to disturb those people that are used to perl flattening of arrays into lists but aren't used to perl's prototypes.