Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Perl 5's function prototypes bad?

People also ask

Should I use function prototypes?

If some function is called somewhere, but its body is not defined yet, that is defined after the current line, then it may generate problems. The compiler does not find what is the function and what is its signature. In that case, we need to function prototypes.

What is a Perl prototype?

A Perl function prototype is zero or more spaces, backslashes, or type characters enclosed in parentheses after the subroutine definition or name. A backslashed type symbol means that the argument is passed by reference, and the argument in that position must start with that type character.

Are there any other advantages of using function prototypes?

Advantages of function prototype : - It helps the compiler in determining whether a function is called correctly or not. Each time when a function is called, its calling statement is compared with its prototype. In case of any mismatch, compiler reports an error.

What are the functions of Perl?

A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.


Prototypes aren't bad if used correctly. The difficulty is that Perl's prototypes don't work the way people often expect them to. People with a background in other programming languages tend to expect prototypes to provide a mechanism for checking that function calls are correct: that is, that they have the right number and type of arguments. Perl's prototypes are not well-suited for this task. It's the misuse that's bad. Perl's prototypes have a singular and very different purpose:

Prototypes allow you to define functions that behave like built-in functions.

  • Parentheses are optional.
  • Context is imposed on the arguments.

For example, you could define a function like this:

sub mypush(\@@) { ... }

and call it as

mypush @array, 1, 2, 3;

without needing to write the \ to take a reference to the array.

In a nutshell, prototypes let you create your own syntactic sugar. For example the Moose framework uses them to emulate a more typical OO syntax.

This is very useful but prototypes are very limited:

  • They have to be visible at compile-time.
  • They can be bypassed.
  • Propagating context to arguments can cause unexpected behavior.
  • They can make it difficult to call functions using anything other than the strictly prescribed form.

See Prototypes in perlsub for all the gory details.


The problem is that Perl's function prototypes don't do what people think they do. Their purpose is to allow you to write functions that will be parsed like Perl's built-in functions.

First of all, method calls completely ignore prototypes. If you're doing OO programming, it doesn't matter what prototype your methods have. (So they shouldn't have any prototype.)

Second, prototypes aren't strictly enforced. If you call a subroutine with &function(...), the prototype is ignored. So they don't really provide any type safety.

Third, they're spooky action-at-a-distance. (Especially the $ prototype, which causes the corresponding parameter to be evaluated in scalar context, instead of the default list context.)

In particular, they make it hard to pass parameters from arrays. For example:

my @array = qw(a b c);

foo(@array);
foo(@array[0..1]);
foo($array[0], $array[1], $array[2]);

sub foo ($;$$) { print "@_\n" }

foo(@array);
foo(@array[0..1]);
foo($array[0], $array[1], $array[2]);

prints:

a b c
a b
a b c
3
b
a b c

along with 3 warnings about main::foo() called too early to check prototype (if warnings are enabled). The problem is that an array (or array slice) evaluated in scalar context returns the length of the array.

If you need to write a function that acts like a built-in, use a prototype. Otherwise, don't use prototypes.

Note: Perl 6 will have completely revamped and very useful prototypes. This answer applies only to Perl 5.


I agree with the above two posters. In general, using $ should be avoided. Prototypes are only useful when using block arguments (&), globs (*), or reference prototypes (\@, \$, \%, \*)


Some people, looking at a Perl subroutine prototype, thinks it means something that it doesn't:

sub some_sub ($$) { ... }

To Perl, that means that the parser expects two arguments. It's Perl's way of letting you create subroutines that behave like built-ins, all of which know what to expect from the succeeding code. You can read about prototypes in perlsub

Without reading the documentation, people guess that the prototypes refer to run time argument checking or something similar that they've seen in other languages. As with most things people guess about Perl, they turn out to be wrong.

However, starting with Perl v5.20, Perl has a feature, experimental as I write this, that gives something more like what users expect and what. Perl's subroutine signatures does run time argument counting, variable assigning, and default setting:

use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);

animals( 'Buster', 'Nikki', 'Godzilla' );

sub animals ($cat, $dog, $lizard = 'Default reptile') { 
    say "The cat is $cat";
    say "The dog is $dog";
    say "The lizard is $lizard";
    }

This is the feature you probably want if you're considering prototypes.