Chapter 3. Intermediate Foundations

Before we get started on the meat of the book, we want to introduce some intermediate-level Perl idioms that we use throughout the book. These are the things that typically set apart the beginning and intermediate Perl programmers. We’ll also introduce the first cast of characters that we’ll use in the examples throughout the book.

List Operators

A list is an ordered collection of scalars. Lists are the values themselves, and sometimes we store lists in arrays, the container that holds an ordered list. List operators do something with multiple elements, and most don’t care if they use a literal list, the return values from a subroutine, or an array variable.

We already know several list operators in Perl, but we may not have thought of them as working with lists. The most common list operator is print. We give it one or more arguments, and it puts them together for us:

print 'Two castaways are ', 'Gilligan', ' and ', 'Skipper', "\n";

There are several other list operators that we already showed in Learning Perl. The sort operator puts its input list in order. In their theme song, the castaways don’t come in alphabetical order, but sort can fix that for us:

my @castaways = sort qw(Gilligan Skipper Ginger Professor Mary-Ann);

The reverse operator returns a list in the opposite order:

my @castaways = reverse qw(Gilligan Skipper Ginger Professor Mary-Ann);

We can even use these operators “in place” by having the same array on both the righthand and lefthand ...

Get Intermediate Perl, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.