Chapter 2. 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. Along the way, we’ll also introduce you to the cast of characters that we’ll use in the examples throughout the book.

List Operators

You already know about several list operators in Perl, but you may not have thought of them as working with lists. The most common list operator is probably 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 you already know about from 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);

Perl has many other operators that work with lists, and, once you get used to them, you’ll find yourself typing less and expressing your intent more clearly.

List Filtering with grep

The grep operator takes a list of values and a “testing expression.” It takes one item after another in the list and places it into the $_ variable. It then evaluates the testing expression ...

Get Intermediate Perl now with O’Reilly online learning.

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