Subroutine signatures in Perl
Named parameters in the declaration itself — stable since Perl 5.36, no @_ unpacking dance.
use strict;
use warnings;
use feature 'signatures';
sub area ($width, $height = $width) {
return $width * $height;
}
sub tag ($label, @items) {
return map { "$label: $_" } @items;
}
print area(3, 4), "\n";
print area(5), "\n";
print "$_\n" for tag("todo", "write", "test");
How it works
use feature 'signatures'turns the syntax on for the file.areanames two parameters, and the default$height = $widthcan read earlier ones — one argument makes a square.- A trailing
@itemsis a slurpy:tagtakes one label, then as many items as you pass.
Keywords and builtins used here
areaformapprintreturnsubtaguse
The run, in numbers
- Lines
- 15
- Characters to type
- 276
- Tokens
- 63
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 50 seconds.
Step 2 of 3 in Subroutines, step 17 of 27 in Language basics.