in reply to perl5 "prototyping": why?

Prototypes in Perl5 are basically good for one thing: Being able to implement push() without looking like you're using references on the caller's side. This is rarely useful, and they tend to cause lots of problems. You can probably safely remove all prototypes from that module, unless some external code is relying on the calling conventions.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re^2: perl5 "prototyping": why?
by rvosa (Curate) on Jul 07, 2005 at 21:01 UTC
    Thanks, I was thinking the same thing. But what if the arguments are complex?
    sub foo (\$\@\%) { my ( $bar, @baz, %something ) = @_; }
    ...erm, I'm just making up that example (obviously), and I never bother with prototypes so this may well be all wrong, but wouldn't @baz suck in the remainder of @_ after $bar if you remove the prototype? Or something like that?
      Perldoc says:
      Any backslashed prototype character represents an actual argument that absolutely must start with that character. The value passed as part of @_ will be a reference to the actual argument given in the subroutine call, obtained by applying \ to that argument.
      So you call foo as foo($a_scalar, @an_array, %a_hash) and you will get automatic reference/dereference magic.

      Dropping the backslash however breaks this behaviour:

      Unbackslashed prototype characters have special meanings. Any unbackslashed @ or % eats all remaining arguments, and forces list context. An argument represented by $ forces scalar context.

      @baz would then indeed gobble up the hash following it.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law