A simple question, I hope...

From A6, "Calling Subroutines", comes the following:

multi push(@array, +$how, [EMAIL PROTECTED]) {...}

    push(@a, how => 'rapidly', 1,2,3);   # OK
    push(@a, 1,2,3);                     # WRONG, $how == 1!

Oops! What you really wanted to say was:

multi push(@array, [EMAIL PROTECTED], +$how) {...}

    push(@a, how => 'rapidly', 1,2,3);   # OK
    push(@a, 1,2,3);                     # OK

Note the gotcha part... if you want to use both named arguments and a variadic list, you must declare the parameters in the signature in a different order than they must appear when actually calling the sub. If you put the signatured params and the actual arguments in the _same_ order, it will break. The reason for this is because, in the first example, the slurpy array has been placed in the named-only zone, _not_ the positional zone.

Clearly, it's going to be a newbie problem, and I guess I'm not understanding why we can't enforce What They Really Meant.

When calling a sub that has both named params and a slurpy list, the slurpy list should always come last. If a sub has both a slurpy hash and a slurpy list, the slurpy list should still always come last. You simply can't credibly have anything after the slurpy list, or it'll be slurped. So args/params must ALWAYS come in this exact order, if they are to be useful:

   sub foo(
       $x,    # required positional
      ?$y,    # optional positional
      +$k,    # optional named
      *%h,    # optional slurpy hash
      *$s,    # optional slurpy scalar
      [EMAIL PROTECTED],    # optional slurpy array
   ) {...}

I guess what I'm not understanding is why you would _EVER_ want [EMAIL PROTECTED] to be in the named-only zone, and presuming you never would, why we can't syntactically / semantically fix the above gotcha so that the params always appear in the "calling" order?

MikeL



Reply via email to