Perl, Perl 5

Perl: Subroutine signatures

Perl 5.20 experimental feature

Simplest example:

use strict;
use warnings;
use v5.20;
use features 'signatures';

sub add($a, $b) {
    return $a + $b;
}

say add(2,2); # 4

Convenient right? Compared to this –

use strict;
use warnings;
use v5.20;

sub add {
    my ($a, $b) = @_;
    return $a + b;
}

say add(2,2); # 4

How about default arguments?

This can be done with –

use strict;
use warnings;
use v5.20;
use feature 'signatures';

sub add($a, $b = 20) {
    return $a + $b;
}

say add(10, 90); # 100
say add(10); # 30

Neat huh? 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s