Subroutine Signatures in Perl

Wednesday, Mar 26, 2025| Tags: Perl

DISCLAIMER: Image is generated using FREE version of ChatGPT.




Subroutine Signatures in Perl


NOTE: All credit for this article goes to the book Perl New Features by brian d foy.

Have you ever missed subroutine signatures in Perl?

I missed it a lot when I used to program in Java.

Then when Raku introduced its coolest subroutine signatures, I started wondering, when would Perl get subroutine signatures?


Early Days


This is how we used to create subroutines and handle parameters in the early days.

You would typically see one of the two common approaches in old Perl code.


sub greet {
    my ($name) = @_;
    print "Hello $name!!\n";
}

greet("Joe");    # Hello Joe!!

Or like this:


sub greet {
    my $name = shift;
    print "Hello $name!!\n";
}

greet("Joe");    # Hello Joe!!

The Arrival of Subroutine Signatures


Fast forward, Perl v5.20 which brought the first glimpse of subroutine signatures.

Although, they were experimental, it was still a step in the right direction.

Now the old code has transformed into something much cleaner.

Looks better, right?


use v5.20;
use experimental qw(signatures);

sub greet($name) {
    say "Hello $name!!";
}

greet("Joe");    # Hello Joe!!

For those, who prefer the traditional way, subroutine signatures are optional, you can still use the old method.


Stability in Perl v5.36


With Perl v5.36, subroutine signatures became stable.

Now we can create subroutine with signatures like this:


use v5.36;

sub greet($name) {
    say "Hello $name!!";
}

greet("Joe");    # Hello Joe!!

This is what I call a complete makeover!

With time, we got used to the new subroutine signatures.

Over time, we have become accustomed to the new subroutine signatures.


Default Values in Parameters


There are many variations and use cases for subroutine signatures.

In this post, I’ll focus on handling default values in parameters.


use v5.36;

sub greet($name = "World") {
    say "Hello $name!!";
}

greet("Joe");    # Hello Joe!!
greet();         # Hello World!!

Remember: If you have multiple parameters, the one with default values should always come after all required parameters.

Let’s make it more interesting with another example:


use v5.36;

sub sum($x, $y = 1) {
    say $x + $y;
}

sum(2, 2);       # 4
sum(2);          # 3

So far so, good.

How about this one?


use v5.36;

sub sum($x, $y = 1) {
    say $x + $y;
}

sum(2, 2);       # 4
sum(2);          # 3
sum(2, undef);   # 2

Enhancements in Perl v5.38


With Perl v5.38, handling default undef or false values become more efficient using the //= or ||= operator.


use v5.38;

sub sum($x, $y //= 1) {
    say $x + $y;
}

sum(2, 2);       # 4
sum(2);          # 3
sum(2, undef);   # 3

If a parameter is undef, the default value now kicks in.

What about false values?


use v5.38;

sub sum($x, $y ||= 1) {
    say $x + $y;
}

sum(2, 2);       # 4
sum(2);          # 3
sum(2, 0);       # 3

As per the subroutine signature behaviour, even a false-value parameter receives the default value.



Happy Hacking!!

SO WHAT DO YOU THINK ?

If you have any suggestions or ideas then please do share with us.

Contact with me