You are on page 1of 6

Perl Toolbox

Perl Exercises
Whats new in Perl 5.20

2 Perl Exercises | Whats new in Perl 5.20 Perl Toolbox (www.perltoolbox.com)

With a new version of Perl released on May 27, 2014, here is a summary of the headline changes
for Perl v5.20. Enjoy!

Subroutine signatures
This is the big one. It's hard to understate how great this is. No more ugly assignment code -
with 5.20 you can write:

use feature 'signatures';
sub echo_chamber ($sound) {
return $sound;
}

Subroutine signatures explained
A subroutine signature is a formal list of parameters for a subroutine. You can declare a
subroutine signature like this:
use experimental 'signatures';
sub echo ($message) {
print "$message\n";
}

In this example "($message)" is the subroutine signature. That indicates that all calls to the
echo subroutine must pass one parameter. When the subroutine is called, the parameter is
assigned to $message and available for use within the scope of the subroutine.
Default values
A signature can also declare default values for its parameters. Let's add a default message to
the echo subroutine:
use experimental 'signatures';
3 Perl Exercises | Whats new in Perl 5.20 Perl Toolbox (www.perltoolbox.com)

sub echo ($message = 'Hello World!') {
print "$message\n";
}

Now the value of $message will default to "Hello World!" when the subroutine is called without
arguments.
Argument checking
Adding a signature to a subroutine enables argument checking for all calls to that subroutine.
For example this code generates an error when run:
use experimental 'signatures';
sub echo ($message) {
print "$message\n";
}
echo(); # missing argument

$ perl echo.pl
Too few arguments for subroutine at echo.pl line 3.

Perl will also raise an error if too many arguments are passed:

use experimental 'signatures';
sub echo ($message) {
print "$message\n";
}
4 Perl Exercises | Whats new in Perl 5.20 Perl Toolbox (www.perltoolbox.com)

echo('hello', 'world'); # too many arguments

$ perl echo.pl
Too many arguments for subroutine at echo.pl line 3.

This is helpful- it avoids the need to write boilerplate argument checking code inside
subroutines. Beware though; as there is no value check, the following will not raise an
arguments error:

use experimental 'signatures';
sub echo ($message) {
print "$message\n";
}
echo(undef); #undef is an argument
Less ugly code
You can banish those unsightly variable assignments from your subroutines. Say goodbye (and
good riddance) to this:
sub ugly_code {
my ($arg1, $arg2, arg3) = @_;
...
}

And say hello to this:
sub fine_code ($arg1, $arg2, arg3) {
5 Perl Exercises | Whats new in Perl 5.20 Perl Toolbox (www.perltoolbox.com)

...
}
Postfix dereferencing
The next cool new feature is postfix dereferencing. Hard to describe but easy to show:
use experimental 'postderef';
my $nested_array_ref = [[[[[1,2,3]]]]];
# circumfix dereference - usual way
push @{$nested_array_ref->[0]->[0]->[0]->[0]}, 4;
# postfix dereference - new way
push $nested_array_ref->[0]->[0]->[0]->[0]->@*, 5;
Hash slices
Perl 5.20 delivers a new slice type: hash slices. These work in a similar way to the array slice,
except the "sliced" data provides full key value pairs instead of just the values as with an array
slice.
my %raindrops = ( splish => 4, splash => 9, splosh => 7 );
my %hash_slice = %raindrops{ 'splish', 'splosh'};
# hash_slice is (splish => 4, splosh => 7)

What's even more cool, if you use a hash slice on an array, the resulting hash has the array
index elements as the keys:
my @raindrop_types = qw/splish splash splosh/;
my %hash_slice = %raindrop_types[0, 2];
# hash_slice is (0 => 'splish', 2 => 'splosh')
6 Perl Exercises | Whats new in Perl 5.20 Perl Toolbox (www.perltoolbox.com)

You might also like