typestar

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

  1. use feature 'signatures' turns the syntax on for the file.
  2. area names two parameters, and the default $height = $width can read earlier ones — one argument makes a square.
  3. A trailing @items is a slurpy: tag takes one label, then as many items as you pass.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Subroutines, step 17 of 27 in Language basics.

← Previous Next →