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.
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
, andforeach
Subroutines
Basic regular expressions
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.
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__
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.
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.
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.
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.
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.
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â¦
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.
[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.
[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.
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.