Skip to Content
Perl 6 Deep Dive
book

Perl 6 Deep Dive

by Andrew Shitov
September 2017
Beginner
402 pages
9h 52m
English
Packt Publishing
Content preview from Perl 6 Deep Dive

Slurpy parameters

The great part of Perl 6 is that it allows passing arrays and hashes in function signatures, Meaning that an array is passed as a single value, not as a list of its values. Consider the following simple example of how the add function can be modified to return the sum of all the elements of an array:

sub add(@arr) {
    [+] @arr
}

my @a = <10 20 30>;
say add(@a); # 60

The [+] construction is a reduced form of the + operator; see the details in Chapter 4, Working with Operators. It returns the sum of all the elements of the @arr array, which is the only argument of the sub.

You may safely add more arguments after the array to the sub. Let's create a function to calculate the sum of the first $n elements of an array:

sub sum_first(@a, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe
Learning Perl 6

Learning Perl 6

brian d foy
Think Perl 6

Think Perl 6

Laurent Rosenfeld, Allen B. Downey
Pro Perl

Pro Perl

Peter Wainwright

Publisher Resources

ISBN: 9781787282049Supplemental Content