subroutines intermediate

How to use subroutine signatures in modern Perl?

Question

How to use subroutine signatures in modern Perl?

Using Subroutine Signatures in Modern Perl

Subroutine signatures are a way to declare expected parameters directly in the subroutine definition, providing cleaner and more readable code than manually unpacking @_. Introduced experimentally in Perl 5.20 and stabilized in Perl 5.24, subroutine signatures allow you to specify parameter names, default values, and even prototypes, clarifying intent and reducing boilerplate.

What are Subroutine Signatures?

Traditionally, Perl subroutines receive arguments in the special array @_. Accessing arguments means indexing into @_, which can be error-prone and less readable:


sub greet {
    my ($name) = @_;            # extract the first argument
    print "Hello, $name\n";
}

With subroutine signatures, you declare parameters explicitly right after the sub name, similar to many other languages:


sub greet($name) {
    print "Hello, $name\n";
}

Perl automatically aliases the passed arguments into the parameter variables.

Enabling Signatures

Signatures are a feature introduced experimentally in Perl 5.20, but stable and recommended to enable from Perl 5.26 onwards. To use them, add:


use feature 'signatures';
no warnings 'experimental::signatures';

This enables support and silences experimental warnings for versions before the feature matured.

Basic Example

Here is a complete, runnable example demonstrating signatures:


use strict;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';

sub greet($name) {
    print "Hello, $name!\n";
}

sub add($x, $y) {
    return $x + $y;
}

greet("Alice");
my $sum = add(10, 5);
print "Sum is $sum\n";

This code prints:


Hello, Alice!
Sum is 15

What Happens Under the Hood?

  • $name and $x, $y are automatically assigned (aliased) from the corresponding @_ values.
  • You don’t have to manually unpack @_.
  • Parameters are lexical variables scoped to the subroutine body.

More Features and Gotchas

  • Parameter context: Signatures are always scalar context. You cannot directly specify arrays or hashes in signatures as of Perl 5.30.
  • Defaults and prototypes: Signatures do not currently support default values or prototypes. You’ll still need to code defaults inside the subroutine.
  • Experimental warning: In Perl versions before 5.26, using signatures triggers an experimental warning unless you disable it (as above).
  • Multiple parameters: You can declare multiple, comma-separated parameters, and the list length is enforced by default — passing fewer or more parameters will cause a runtime error.
  • Compatibility: Older Perls (before 5.20) do not support signatures.

Summary

Subroutine signatures make your Perl code cleaner by explicitly naming subroutine parameters and eliminating manual unpacking of @_. They were introduced in Perl 5.20 as an experimental feature and stabilized since 5.26. To use them, enable use feature 'signatures' and disable experimental warnings on older versions. Keep in mind that signatures have some limitations (no array/hash parameters, no defaults), so they work best for simple, scalar-parameter routines.

Overall, adopting signatures modernizes your Perl subroutines, improves readability, and helps avoid common bugs related to argument handling.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 6ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
(empty)
STDERR
(empty)

Was this helpful?

Related Questions