My thinking about Subroutine Signature in 2019.

My thinking about Subroutine Signature in 2019.

Rethink the syntax of @ and%

Subroutine signature syntax should be simple and small.

One blame for Perl is that Perl is complex.

The introduction of new symbols such as "@" and "%" complicate Perl.

At first glance, you will not know what you are doing.

sub func($foo, $bar, @) {
  # What is "@"?
}

sub func($foo, $bar, %) {
# What is "%"?
}

Allow only scalar variables and do not check the number of arguments

Having lots of features is useful, but complicates Perl's grammar.

I prefer the simple syntax of JavaScript. Only scalar variables are allowed. The number of arguments is not checked. In this case, you cannot access @_.

sub func($foo, $bar) {
  # Only support scalar variables
}

This syntax is efficient because it accepts arguments in one OP. This is the recommended way for optimizing the process of receiving arguments.

If you want to access the argument as array, choose not to use the subroutine signature.

sub func {
  my ($foo, $bar, @nums) = @_;
}

Ensuring forward compatibility

I hope subroutine signature syntax works in Perl 5.8.7 with the help of the signature module.


use signature;

sub func($foo, $bar) {
# Work well in Perl 5.8.7+
}

Ensuring forward compatibility of Perl 5.8.7.

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.