Search code examples
perlprototypesubroutine

Are there any drawbacks to using Perl's prototypes?


I would to call my subs without using parentheses around the argument lists. For example, I'd like to use

mypush \@list, $item;

instead of

mypush(\@list, $item);

To accomplish that, I've declared my sub with a prototype.

sub mypush (+@) { push shift, @_ }

In fact, that also allows me to the following, but that's just a bonus:

mypush @list, $item;

Other than having to declare subroutines with prototypes before using them anywhere in the code, is there any serious drawback to using prototypes in Perl?

Code looks definitely cleaner to me. But where in general can prototypes go wrong? When would I regret it?


Solution

  • One of the biggest drawbacks is that it makes the code harder to maintain. Very few people seem to understand what a Perl prototype actually does, so there is a lot of guessing and cargo-culting around them.

    Note, though, that Perl 5.20 has added a subroutine signature feature. It's still experimental, but it's really kinda awesome and what most people expect prototypes to do. And, when we get type constraints added (oh Santa, please please please bring me those for Christmas I promise I'll be good!), we'll have what I think most people try to do with prototypes (with some syntax I make up):

    sub foo (Int $foo, String $var, Int @array) { ... }