Chapter 1. Introduction

Welcome to the next step in your understanding of Perl. You’re probably here either because you want to learn to write programs that are more than 100 lines long or because your boss has told you to do so.

Our Learning Perl book was great because it introduced the use of Perl for short and medium programs (which is most of the programming done in Perl, we’ve observed). But, to keep “the Llama book” from being big and intimidating, we deliberately and carefully left a lot of information out.

In the pages that follow, you can get “the rest of the story” in the same style as our friendly Llama book. It covers what you need to write programs that are 100 to 10,000 (or even longer) lines long.

For example, you’ll learn how to work with multiple programmers on the same project by writing reusable Perl modules that you can wrap in distributions usable by the common Perl tools. This is great, because unless you work 35 hours each day, you’ll need some help with larger tasks. You’ll also need to ensure that your code all fits with the other code as you develop it for the final application.

This book will also show you how to deal with larger and more complex data structures, such as what we might casually call a “hash of hashes” or an “array of arrays of hashes of arrays.” Once you know a little about references, you’re on your way to arbitrarily complex data structures, which can make your life much easier.

Then there’s the buzzworthy notion of object-oriented programming, which allows parts of your code (or hopefully code from others) to be reused with minor or major variations within the same program. The book will cover that as well, even if you’ve never seen objects before.

An important aspect of working in teams is having a release cycle and a process for unit and integration testing. You’ll learn the basics of packaging your code as a distribution and providing unit tests for that distribution, both for development and for verifying that your code works in your target environment.

And, just as was promised and delivered in Learning Perl, we’ll entertain you along the way by interesting examples and bad puns. We’ve sent Fred and Barney and Betty and Wilma home, though. A new cast of characters will take the starring roles.

What Should You Know Already?

We’ll presume that you’ve already read Learning Perl, using at least the fifth edition, or at least pretend you have, and that you’ve played enough with Perl to already have those basics down. For example, you won’t see an explanation in this book that shows how to access the elements of an array or return a value from a subroutine.

Make sure you know the following things, all of which we covered in Learning Perl:

  • How to run a Perl program on your system

  • The three basic Perl variable types: scalars, arrays, and hashes

  • Control structures such as while, if, for, and foreach

  • Subroutines

  • Basic regular expressions

  • List operators such as grep, map, sort, and print

  • File manipulation such as open, file reading, and −X (file tests)

You might pick up deeper insight into these topics in this book, but we’re going to presume you know the basics.

The final parts of this book deal with distributions and contributing to CPAN. To do that, you should apply for a PAUSE account now so it’s ready to use when you get there. Request an account at https://pause.perl.org/pause/query?ACTION=request_id.

strict and warnings

We introduced the strict and warnings pragmas in Learning Perl, and we expect that you’ll use them for all of your code. However, for most of the code that you’ll see in this book, assume that we’ve already turned on strict and warnings so we don’t distract from the examples with repeated boilerplate code, just like we leave off the shebang line and the usual documentation bits. When we present full examples, we’ll include these pragmas as well.

You might want to do what we do. Instead of starting a program from scratch, we open a template that has the usual bits of code in it. Until you develop your own template, complete with standard documentation and your favorite way of doing things, you can start with this simple one that you can assume is around all of our code examples:

#!/usr/local/bin/perl
use strict;
use warnings;

__END__

Perl v5.14

This book is current up to at least Perl v5.14, released in 2011. Usually, the details of the language are stable within the version. Some of the modules we use might have updates, especially since many dual-lived modules that come with Perl also show up separately on CPAN. Since we generally present the basic ideas of Perl and usually only brief overviews of modules, you should always check the modules’ documentation for any updates.

Note

As we finish writing in the middle of 2012, Perl v5.16 is going to be released about a week after we turn this book in to the publisher, and we may have snuck some of those features in the book.

Some of the newer features require us to explicitly state that we want to use them so that they don’t disturb programs targeting earlier versions of Perl. The easiest way to enable these features is to tell Perl which version we require. The number 5.014 has to have three digits after the decimal point (in case there is ever a Perl 5.140):

use 5.014;

say "Hello World!";

You can also write this with the v notation and its multiple parts:

use v5.14.2;

With the double-dotted form, we could leave off the v:

use 5.14.2;

But, that leaves us the temptation to leave it off in all cases.

Whenever we write some code that requires a feature from a specific version of perl, we’ll insert that use v5.14 line (or whatever the appropriate version is) using the first version that made that feature available. If we can, we’ll also show a version of the code that can work with earlier versions of Perl. We consider Perl v5.8, first released in 2002, to be the earliest version that anyone should use, so code samples that don’t specify a version assume Perl v5.8. In general, we strive to write code that works for as many people and as many versions of Perl as possible, but we also want you to be as up-to-date as you wish to be.

To learn more about some of the basics of Perl v5.14, you might want to check out Learning Perl, Sixth Edition.

A Note on Versions

In this book, we write the Perl version as v5.M.N, with the leading v. So far, we’ve also prefixed the version with “Perl,” but that’s going to get tedious as we mention version differences. Instead, we’ll leave off the Perl for this point on. When we say “v5.14.2,” we’re talking about Perl 5.14.2. That’s the current maintenance version as we write this book, although v5.16 is right around the corner.

The number after the v5 can be either odd or even, and these distinguish between the experimental and maintenance versions. The maintenance version, such as v5.14, is for normal users and production use. The experimental version, such as v5.15, is where the Perl 5 Porters add new features, reimplement or optimize code, and make other unstable changes. When they are ready, they graduate the experimental version to a maintenance version by bumping that second number to the next higher even number.

The third number, the 2 in v5.14.2 for instance, is a point release. When we say v5.14, our point should apply to all point releases in that version. Sometimes, we need to denote a particular version; from Learning Perl, you might remember that between v5.10.0 and v5.10.1, smart matching fixed a serious design bug and changed behavior.

This book is strictly about v5. There’s another thing, sometimes called Perl v6, but that’s related to v5 tangentially. It’s designed as a new language specification and is also designed by Larry Wall, but it’s not an upgrade to v5 (even if, in 2000, we thought it might be). We know that’s confusing, and so do the v6 people, which is why the implementations of the v6 specification have been given different names, such as Rakudo and Niecza.

What About All Those Footnotes?

Like Learning Perl, this book relegates some of the more esoteric items out of the way for the first reading and places those items in footnotes.[3] You should skip those the first time through and pick them up on a rereading. You will not find anything in a footnote that you’ll need to understand any of the material we present later.

What’s With the Exercises?

It’s critical that you do the exercises. Hands-on training gets the job done better. The best way to provide this training is with a series of exercises after every half hour to hour of presentation. If you’re a speed reader, the end of the chapter may come a bit sooner than a half hour. Slow down, take a breather, and do the exercises!

Each exercise has a “minutes to complete” rating. We intend for this rating to hit the midpoint of the bell curve, but don’t feel bad if you take more or less time. Sometimes it’s just a matter of how many times you’ve faced similar programming tasks in your studies or jobs. Use the numbers merely as a guideline.

Every exercise has its answer in the Appendix A. Again, try not to peek; you’ll ruin the value of the exercise.

How to Get Help

As the book authors, we’re always glad to help when we can, but we’re already inundated with more email than we can manage. There are several online resources where you can get help, either from us directly, or from many of the other helpful people in the Perl community.

Stack Overflow (http://www.stackoverflow.com/)

Stack Overflow is a no-pay question-and-answer site for all sorts of programming questions, and there are many clueful Perlers who regularly answer your questions. You’re likely to get excellent answers within an hour and for free. You might even get an answer from one of the authors.

Perlmonks (http://www.perlmonks.org/)

Perlmonks is an online Perl community where you can ask questions, post your thoughts on Perl, and interact with other Perlers. If you have a question regarding something about Perl, people have probably already discussed it at Perlmonks. You can search the archives or start a new thread.

learn@perl.org and http://learn.perl.org/

The learn@perl.org mailing list is specifically designed as a safe place for Perl neophytes to ask questions without fear that you are bothering anyone. It’s just waiting for your questions, no matter how basic you think they are.

module-authors@perl.org

If your question is specifically about writing and distributing modules, there’s a special mailing list for that: module-authors@perl.org.

comp.lang.perl.misc

If Usenet is more of your thing, you can ask questions on comp.lang.perl.misc. Several longtime Perl users monitor the group, and sometimes they are even helpful.

What If I’m a Perl Course Instructor?

If you’re a Perl instructor who has decided to use this as your textbook, you should know that each set of exercises is short enough for most students to complete the whole set in 45 minutes to an hour, with a little time left over for a break. Some chapters’ exercises should be quicker, and some may take longer. That’s because once all those little numbers in square brackets were written, we discovered that we don’t know how to add.

So let’s get started. Class begins after you turn the page…

Exercises

At the end of each chapter, we’ve included exercises like these. Before each exercise, we show the time we think it will take most people to complete the exercise. If you take longer, that’s just fine, at least until we figure out how to make ebooks with timers.

You can find the answers to this exercises in Answers for Chapter 1.

  1. [5 minutes] Get a PAUSE account by requesting it from http://pause.perl.org/. You’ll need this for the last chapter in the book, and we want it waiting for you.

  2. [5 minutes] Visit this book’s website, http://www.intermediateperl.com/. You should be especially interested in the Download section, which has files useful for the exercises. Download the archive so you have it even if you don’t have Internet access later.



[3] Like this.

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.