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

Thanks but that still is a bit confusing. It counts and characterizes arguements? Does that mean it's not looking for anything in particular? It'll accept any input?
  • Comment on Re: Re: What is inside of () on a sub call?

Replies are listed 'Best First'.
Re: Re: Re: What is inside of () on a sub call?
by Zaxo (Archbishop) on May 22, 2004 at 04:30 UTC

    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

      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.