O'Reilly logo

Learning Perl, 6th Edition by Tom Phoenix, Randal L. Schwartz, brian d foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Introduction

Welcome to the Llama book!

This is the sixth edition of a book that has been enjoyed by over half a million readers since 1993. At least, we hope they’ve enjoyed it. It’s a sure thing that we enjoyed writing it.[4]

Questions and Answers

You probably have some questions about Perl, and maybe even some about this book; especially if you’ve already flipped through it to see what’s coming. So, we’ll use this chapter to answer them, including how to find answers that we don’t provide.

Is This the Right Book for You?

If you’re anything like us, you probably didn’t get to browse this book before you bought it. As we finish up this edition, the bookstore Borders is closing many of its stores and other booksellers aren’t doing much better. You might be reading this book in a digital form that you downloaded, or as HTML in Safari Books Online. How can you find out if this book is the one you want to buy if you can’t look at it first? How can we warn you off if you need to buy the book to read this paragraph?

This is not a reference book. It’s a tutorial on the very basics of Perl, which is just enough for you to create simple programs mostly for your own use. We don’t cover every detail of every topic, and we spread out some of the topics over several chapters so you pick up concepts as you need them.

Our intended readers are people who know at least a little bit about programming and just need to learn Perl. We assume that you have at least some background in using a terminal, editing files, and running programs—just not Perl programs. You already know about variables and subroutines and the like, but you just need to see how Perl does it.

This doesn’t mean that the absolute beginner, having never touched a terminal program or written a single line of code, will be completely lost. You might not catch everything we say the first time you go through the book, but many beginners have used the book with only minor frustrations. The trick is to not worry about everything you might be missing and to focus on just the core concepts we present. You might take a little longer than an experienced programmer, but you have to start somewhere.

And, this shouldn’t be the only Perl book you ever read. It’s just a tutorial. It’s not comprehensive. It gets you started in the right direction so you can go on to our other books, Intermediate Perl (at the time of this writing, the second edition is forthcoming) and Mastering Perl, when you are ready. The definitive reference for Perl is Programming Perl, also known as the “Camel book.”

We should also note that even though this book covers up to Perl 5.14, it’s still useful even if you have an earlier version. You might miss out on some of the cool new features, but you’ll still learn how to use basic Perl. The least recent version that we’ll think about, however, is Perl 5.8, even though that was released almost 10 years ago.

Why Are There So Many Footnotes?

Thank you for noticing. There are a lot of footnotes in this book. Ignore them. They’re needed because Perl is chock-full of exceptions to its rules. This is a good thing, as real life is chock-full of exceptions to rules.

But it means that we can’t honestly say, “The fizzbin operator frobnicates the hoozistatic variables” without a footnote giving the exceptions.[5] We’re pretty honest, so we have to write the footnotes. But you can be honest without reading them. (It’s funny how that works out.) The footnotes are extra information that you don’t need for the core concepts.

Many of the exceptions have to do with portability. Perl began on Unix systems, and it still has deep roots in Unix. But wherever possible, we’ve tried to show when something may behave unexpectedly, whether that’s because it’s running on a non-Unix system or for another reason. We hope that readers who know nothing about Unix will nevertheless find this book a good introduction to Perl. (And they’ll learn a little about Unix along the way, at no extra charge.)

And many of the other exceptions have to do with the old “80/20” rule. By that we mean that 80% of the behavior of Perl can be described in 20% of the documentation, and the other 20% of the behavior takes up the other 80% of the documentation. So, to keep this book small, we’ll talk about the most common, easy-to-talk-about behavior in the main text, and hint in the direction of the other stuff in the footnotes (which are in a smaller font, so we can say more in the same space).[6] Once you’ve read the book all the way through without reading the footnotes, you’ll probably want to look back at some sections for reference. At that point, or if you become unbearably curious along the way, go ahead and read the notes. A lot of them are just computer jokes anyway.

What About the Exercises and Their Answers?

The exercises are at the end of each chapter because, between the three of us, we’ve presented this same course material to several thousand students.[7] We have carefully crafted these exercises to give you the chance to make mistakes as well.

It’s not that we want you to make mistakes, but you need to have the chance. That’s because you are going to make most of these mistakes during your Perl programming career, and it may as well be now. Any mistake that you make while reading this book you won’t make again when you’re writing a program on a deadline. And we’re always here to help you out if something goes wrong, in the form of Appendix A, which has our answers for each exercise and a little text to go with it, explaining the mistakes you made and a few you didn’t. Check out the answers when you’re done with the exercises.

Try not to peek at the answer until you’ve given the problem a good try, though. You’ll learn better if you figure it out rather than read about it. Don’t knock your head repeatedly against the wall if you don’t figure out a solution: move on to the next chapter and don’t worry too much about it.

Even if you never make any mistakes, you should look at the answers when you’re done; the accompanying text will point out some details of the program that might not be obvious at first.

If you want additional exercises, check out the Learning Perl Student Workbook, which adds several exercises for each chapter.

What Do Those Numbers Mean at the Start of the Exercise?

Each exercise has a number in square brackets in front of the exercise text, looking something like this:

  1. [2] What does the number 2 inside square brackets mean, when it appears at the start of an exercise’s text?

That number is our (very rough) estimate of how many minutes you can expect to spend on that particular exercise. It’s rough, so don’t be too surprised if you’re all done (with writing, testing, and debugging) in half that time, or not done in twice that long. On the other hand, if you’re really stuck, we won’t tell anyone that you peeked at Appendix A to see what our answer looked like.

What If I’m a Perl Course Instructor?

If you’re a Perl instructor who has decided to use this as your textbook (as many have over the years), you should know that we’ve tried to make each set of exercises short enough that most students could do 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 we had written all of those little numbers in square brackets, we discovered that we don’t know how to add (luckily we know how to make computers do it for us).

We also have a companion book, the Learning Perl Student Workbook, which has additional exercises for each chapter. If you get the version of the workbook for the fourth edition, you will have to adjust the chapter order because in this edition, we have added a chapter and moved another.

What Does “Perl” Stand For?

Perl is sometimes called the “Practical Extraction and Report Language,” although it has also been called a “Pathologically Eclectic Rubbish Lister,” among other expansions. It’s actually a backronym, not an acronym, since Larry Wall, Perl’s creator, came up with the name first and the expansion later. That’s why “Perl” isn’t in all caps. There’s no point in arguing which expansion is correct: Larry endorses both.

You may also see “perl” with a lowercase p in some writing. In general, “Perl” with a capital P refers to the language and “perl” with a lowercase p refers to the actual interpreter that compiles and runs your programs. In the house style, we write the names of programs like perl.

Why Did Larry Create Perl?

Larry created Perl in the mid-1980s when he was trying to produce some reports from a Usenet-news-like hierarchy of files for a bug-reporting system, and awk ran out of steam. Larry, being the lazy programmer that he is,[8] decided to overkill the problem with a general-purpose tool that he could use in at least one other place. The result was Perl version zero.

Why Didn’t Larry Just Use Some Other Language?

There’s no shortage of computer languages, is there? But, at the time, Larry didn’t see anything that really met his needs. If one of the other languages of today had been available back then, perhaps Larry would have used one of those. He needed something with the quickness of coding available in shell or awk programming, and with some of the power of more advanced tools like grep, cut, sort, and sed,[9] without having to resort to a language like C.

Perl tries to fill the gap between low-level programming (such as in C or C++ or assembly) and high-level programming (such as “shell” programming). Low-level programming is usually hard to write and ugly, but fast and unlimited; it’s hard to beat the speed of a well-written low-level program on a given machine. And there’s not much you can’t do there. High-level programming, at the other extreme, tends to be slow, hard, ugly, and limited; there are many things you can’t do at all with the shell or batch programming if there’s no command on your system that provides the needed functionality. Perl is easy, nearly unlimited, mostly fast, and kind of ugly.

Let’s take another look at those four claims we just made about Perl.

First, Perl is easy. As you’ll see, though, this means it’s easy to use. It’s not especially easy to learn. If you drive a car, you spent many weeks or months learning how, and now it’s easy to drive. When you’ve been programming Perl for about as many hours as it took you to learn to drive, Perl will be easy for you.[10]

Perl is nearly unlimited. There are very few things you can’t do with Perl. You wouldn’t want to write an interrupt-microkernel-level device driver in Perl (even though that’s been done), but most things that ordinary folks need most of the time are good tasks for Perl, from quick little one-off programs to major industrial-strength applications.

Perl is mostly fast. That’s because nobody is developing Perl who doesn’t also use it—so we all want it to be fast. If someone wants to add a feature that would be really cool but would slow down other programs, Larry is almost certain to refuse the new feature until we find a way to make it quick enough.

Perl is kind of ugly. This is true. The symbol of Perl has become the camel, from the cover of the venerable Camel book (also known as Programming Perl), a cousin of this book’s Llama (and her sister, the Alpaca). Camels are kind of ugly, too. But they work hard, even in tough conditions. Camels are there to get the job done despite all difficulties, even when they look bad and smell worse and sometimes spit at you. Perl is a little like that.

Is Perl Easy or Hard?

Perl is easy to use, but sometimes hard to learn. This is a generalization, of course. In designing Perl, Larry made many trade-offs. When he’s had the chance to make something easier for the programmer at the expense of being more difficult for the student, he’s decided in the programmer’s favor nearly every time. That’s because you’ll learn Perl only once, but you’ll use it again and again.[11] Perl has any number of conveniences that let the programmer save time. For example, most functions will have a default; frequently, the default is the way that you’ll want to use the function. So you’ll see lines of Perl code like these:[12]

while (<>) {
    chomp;
    print join("\t", (split /:/)[0, 2, 1, 5] ), "\n";
}

Written out in full, without using Perl’s defaults and shortcuts, that snippet would be roughly ten or twelve times longer, so it would take much longer to read and write. It would be harder to maintain and debug, too, with more variables. If you already know some Perl, and you don’t see the variables in that code, that’s part of the point. They’re all being used by default. But to have this ease at the programmer’s tasks means paying the price when you’re learning; you have to learn those defaults and shortcuts.

A good analogy is the proper and frequent use of contractions in English. Sure, “will not” means the same as “won’t.” But most people say “won’t” rather than “will not” because it saves time, and because everybody knows it and it makes sense. Similarly, Perl’s “contractions” abbreviate common “phrases” so that they can be “spoken” quicker and understood by the maintainer as a single idiom, rather than a series of unrelated steps.

Once you become familiar with Perl, you may find yourself spending less time trying to get shell quoting (or C declarations) right, and more time surfing the Web because Perl is a great tool for leverage. Perl’s concise constructs allow you to create (with minimal fuss) some very cool one-up solutions or general tools. Also, you can drag those tools along to your next job because Perl is highly portable and readily available, so you’ll have even more time to surf.

Perl is a very high-level language. That means that the code is quite dense; a Perl program may be around a quarter to three-quarters as long as the corresponding program in C. This makes Perl faster to write, faster to read, faster to debug, and faster to maintain. It doesn’t take much programming before you realize that, when the entire subroutine is small enough to fit onscreen all at once, you don’t have to keep scrolling back and forth to see what’s going on. Also, since the number of bugs in a program is roughly proportional to the length of the source code[13] (rather than being proportional to the program’s functionality), the shorter source in Perl will mean fewer bugs on average.

Like any language, Perl can be “write-only”—it’s possible to write programs that are impossible to read. But with proper care, you can avoid this common accusation. Yes, sometimes Perl looks like CPAN line-noise to the uninitiated, but to the seasoned Perl programmer, it looks like the notes of a grand symphony. If you follow the guidelines of this book, your programs should be easy to read and easy to maintain, and they probably won’t win The Obfuscated Perl Contest.

How Did Perl Get to Be So Popular?

After playing with Perl a bit, adding stuff here and there, Larry released it to the community of Usenet readers, commonly known as “the Net.” The users on this ragtag fugitive fleet of systems around the world (tens of thousands of them) gave him feedback, asking for ways to do this, that, or the other thing, many of which Larry had never envisioned his little Perl handling.

But as a result, Perl grew, and grew, and grew. It grew in features. It grew in portability. What was once a little language available on only a couple of Unix systems now has thousands of pages of free online documentation, dozens of books, several mainstream Usenet newsgroups (and a dozen newsgroups and mailing lists outside the mainstream) with an uncountable number of readers, and implementations on nearly every system in use today—and don’t forget this Llama book as well.

What’s Happening with Perl Now?

Larry Wall doesn’t write the code these days, but he still guides the development and makes the big decisions. Perl is mostly maintained by a hardy group of people called the Perl 5 Porters. You can follow their work and discussions on the mailing list.

As we write this (March 2011), there is a lot happening with Perl. For the past couple of years, many people have been working on the next major version of Perl: Perl 6.

In short, Perl 6 is a completely different language now, even to the point that its main implementation goes by the name Rakudo. In 2000, Perl 6 started as something that might replace Perl 5, which had been in the doldrums with long lag times in the releases of Perl 5.6, 5.8, and 5.10. Through various accidents and tangents, it turned out that as Perl 5 hotted up again, Perl 6 bogged down. Ironic, perhaps?

However, Perl 5 development was also revitalized and now has monthly releases of experimental versions and roughly yearly releases of new maintenance versions. The last edition of this book covered 5.10, and there wasn’t time to update it before Perl 5.12 came out. Now this book is available right around the time Perl 5.14 should be released, with the Perl 5 Porters already thinking about Perl 5.16.

What’s Perl Really Good For?

Perl is good for quick-and-dirty programs that you whip up in three minutes. Perl is also good for long-and-extensive programs that will take a dozen programmers three years to finish. Of course, you’ll probably find yourself writing many programs that take you less than an hour to complete, from the initial plan to the fully tested code.

Perl is optimized for problems which are about 90% working with text and about 10% everything else. That description seems to fit most programming tasks that pop up these days. In a perfect world, every programmer would know every language; you’d always be able to choose the best language for each project. Most of the time, you’d choose Perl.[14] Although the Web wasn’t even a twinkle in Tim Berners-Lee’s eye when Larry created Perl, it was a marriage made on the Net. Some claim that the deployment of Perl in the early 1990s permitted people to move lots of content into HTML format very rapidly, and the Web couldn’t exist without content. Of course, Perl is the darling language for small CGI scripting (programs run by a web server) as well—so much so that many of the uninformed still make statements like “Isn’t CGI just Perl?” or “Why would you use Perl for something other than CGI?” We find those statements amusing.

What Is Perl Not Good For?

So, if it’s good for so many things, what is Perl not good for? Well, you shouldn’t choose Perl if you’re trying to make an opaque binary. That’s a program that you could give away or sell to someone who then can’t see your secret algorithms in the source, and thus can’t help you maintain or debug your code either. When you give someone your Perl program, you’ll normally be giving them the source, not an opaque binary.

If you’re wishing for an opaque binary, though, we have to tell you that they don’t exist. If someone can install and run your program, they can turn it back into source code. Granted, this won’t necessarily be the same source that you started with, but it will be some kind of source code. The real way to keep your secret algorithm a secret is, alas, to apply the proper number of attorneys; they can write a license that says “you can do this with the code, but you can’t do that. And if you break our rules, we’ve got the proper number of attorneys to ensure that you’ll regret it.”

How Can I Get Perl?

You probably already have it. At least, we find Perl wherever we go. It ships with many systems, and system administrators often install it on every machine at their site. But if you can’t find it already on your system, you can still get it for free. It comes pre-installed with most Linux or *BSD systems, Mac OS X, and some others. Companies such as ActiveState (http://www.activestate.com) provide pre-built and enhanced distributions for several platforms, including Windows. You can also get Strawberry Perl for Windows (http://www.strawberryperl.com), which comes with all the same stuff as regular Perl plus extra tools to compile and install third-party modules.

Perl is distributed under two different licenses. For most people, since you’ll merely be using it, either license is as good as the other. If you’ll be modifying Perl, however, you’ll want to read the licenses more closely, because they put some small restrictions on distributing the modified code. For people who won’t modify Perl, the licenses essentially say, “It’s free—have fun with it.”

In fact, it’s not only free, but it runs rather nicely on nearly everything that calls itself Unix and has a C compiler. You download it, type a command or two, and it starts configuring and building itself. Or, better yet, you get your system administrator to type those two commands and install it for you.[15] Besides Unix and Unix-like systems, people addicted to Perl have ported it to other systems, such as Mac OS X, VMS, OS/2, even MS/DOS, and every modern species of Windows—and probably even more by the time you read this.[16] Many of these ports of Perl come with an installation program that’s even easier to use than the process for installing Perl on Unix. Check for links in the “ports” section on CPAN.

What Is CPAN?

CPAN is the Comprehensive Perl Archive Network, your one-stop shopping for Perl. It has the source code for Perl itself, ready-to-install ports of Perl to all sorts of non-Unix systems,[17] examples, documentation, extensions to Perl, and archives of messages about Perl. In short, CPAN is comprehensive.

CPAN is replicated on hundreds of mirror machines around the world; start at http://search.cpan.org/ to browse or search the archive. If you don’t have access to the Net, you might find a CD-ROM or DVD-ROM with all of the useful parts of CPAN on it; check with your local technical bookstore. Look for a recently minted archive, though. Since CPAN changes daily, an archive from two years ago is an antique. Better yet, get a kind friend with Net access to burn you one with today’s CPAN.

How Can I Get Support for Perl?

Well, you get the complete source—so you get to fix the bugs yourself!

That doesn’t sound so good, does it? But it really is a good thing. Since there’s no “source code escrow” on Perl, anyone can fix a bug—in fact, by the time you’ve found and verified a bug, someone else has probably already got a fix for it. There are thousands of people around the world who help maintain Perl.

Now, we’re not saying that Perl has a lot of bugs. But it’s a program, and every program has at least one bug. To see why it’s so useful to have the source to Perl, imagine that instead of using Perl, you licensed a programming language called Forehead from a giant, powerful corporation owned by a zillionaire with a bad haircut. (This is all hypothetical. Everyone knows there’s no such programming language as Forehead.) Now think of what you can do when you find a bug in Forehead. First, you can report it. Second, you can hope—hope that they fix the bug, hope that they fix it soon, hope that they won’t charge too much for the new version. You can hope that the new version doesn’t add new features with new bugs, and hope that the giant company doesn’t get broken up in an antitrust lawsuit.

But with Perl, you’ve got the source. In the rare and unlikely event that you can’t get a bug fixed any other way, you can hire a programmer or ten and get to work. For that matter, if you buy a new machine that Perl doesn’t yet run on, you can port it yourself. Or if you need a feature that doesn’t yet exist, well, you know what to do.

Are There Any Other Kinds of Support?

Sure. One of our favorites is the Perl Mongers. This is a worldwide association of Perl users’ groups; see http://www.pm.org/ for more information. There’s probably a group near you with an expert or someone who knows an expert. If there’s no group, you can easily start one.

Of course, for the first line of support, you shouldn’t neglect the documentation. Besides the included documentation, you can also read the documentation on CPAN, http://www.cpan.org, as well as other sites—http://perldoc.perl.org has HTML and PDF versions of the Perl documentation, and http://faq.perl.org/ has the latest version of the perlfaq.

Another authoritative source is the book Programming Perl, commonly called “the Camel book” because of its cover animal (just as this book is known as “the Llama book”). The Camel book contains the complete reference information, some tutorial stuff, and a bunch of miscellaneous information about Perl. There’s also a separate pocket-sized Perl 5 Pocket Reference by Johan Vromans (O’Reilly) that’s handy to keep at hand (or in your pocket).

If you need to ask a question of someone, there are newsgroups on Usenet and any number of mailing lists.[18] At any hour of the day or night, there’s a Perl expert awake in some time zone answering questions on Usenet’s Perl newsgroups—the sun never sets on the Perl empire. This means that if you ask a question, you’ll often get an answer within minutes. And if you didn’t check the documentation and FAQ first, you’ll get flamed within minutes.

The official Perl newsgroups on Usenet are located in the comp.lang.perl.* part of the hierarchy. As of this writing, there are five of them, but they change from time to time. You (or whoever is in charge of Perl at your site) should generally subscribe to comp.lang.perl.announce, which is a low-volume newsgroup just for important announcements about Perl, including any security-related announcements. Ask your local expert if you need help with Usenet.

Also, a few web communities have sprung up around Perl discussions. One very popular one, known as The Perl Monastery (http://www.perlmonks.org), has seen quite a bit of participation from many Perl book and column authors, including at least two of the authors of this book. There is also good Perl support on Stack Overflow (http://www.stackoverflow.com).

You can also check out http://learn.perl.org/ and its associated mailing list, . Many well-known Perl programmers also have blogs that regularly feature Perl-related posts, most of which you can read through Perlsphere, http://perlsphere.net/.

If you find yourself needing a support contract for Perl, there are a number of firms who are willing to charge as much as you’d like. In most cases, these other support avenues will take care of you for free.

What If I Find a Bug in Perl?

The first thing to do when you find a bug is to check the documentation[19] again.[20] Perl has so many special features and exceptions to rules that you may have discovered a feature, not a bug. Also, check that you don’t have an older version of Perl; maybe you found something that’s been fixed in a more recent version.

Once you’re 99% certain that you’ve found a real bug, ask around. Ask someone at work, at your local Perl Mongers meeting, or at a Perl conference. Chances are, it’s still a feature, not a bug.

Once you’re 100% certain that you’ve found a real bug, cook up a test case. (What, you haven’t done so already?) The ideal test case is a tiny self-contained program that any Perl user could run to see the same (mis-)behavior as you’ve found. Once you’ve got a test case that clearly shows the bug, use the perlbug utility (which comes with Perl) to report the bug. That will normally send email from you to the Perl developers, so don’t use perlbug until you’ve got your test case ready.

Once you’ve sent off your bug report, if you’ve done everything right, it’s not unusual to get a response within minutes. Typically, you can apply a simple patch and get right back to work. Of course, you may (at worst) get no response at all; the Perl developers are under no obligation to read your bug reports. But all of us love Perl, so nobody likes to let a bug escape our notice.

How Do I Make a Perl Program?

It’s about time you asked (even if you didn’t). Perl programs are text files; you can create and edit them with your favorite text editor. You don’t need any special development environment, although there are some commercial ones available from various vendors. We’ve never used any of these enough to recommend them (but long enough to stop using them). Besides, your environment is a personal choice. Ask three programmers what you should use and you’ll get eight answers.

You should generally use a programmers’ text editor, rather than an ordinary editor. What’s the difference? Well, a programmers’ text editor will let you do things that programmers need, like indenting or un-indenting a block of code, or finding the matching closing curly brace for a given opening curly brace. On Unix systems, the two most popular programmers’ editors are emacs and vi (and their variants and clones). BBEdit and TextMate are good editors for Mac OS X, and a lot of people have said nice things about UltraEdit and PFE (Programmer’s Favorite Editor) on Windows. The perlfaq3 documentation lists several other editors, too. Ask your local expert about text editors on your system.

For the simple programs you’ll write for the exercises in this book, none of which should be more than about 20 or 30 lines of code, any text editor will be fine.

Some beginners try to use a word processor instead of a text editor. We recommend against this—it’s inconvenient at best and impossible at worst. But we won’t try to stop you. Be sure to tell the word processor to save your file as “text only”; the word processor’s own format will almost certainly be unusable. Most word processors will probably also tell you that your Perl program is spelled incorrectly and you should use fewer semicolons.

In some cases, you may need to compose the program on one machine, then transfer it to another to run it. If you do this, be sure that the transfer uses “text” or “ASCII” mode, and not “binary” mode. This step is needed because of the different text formats on different machines. Without it, you may get inconsistent results—some versions of Perl actually abort when they detect a mismatch in the line endings.

A Simple Program

According to the oldest rule in the book, any book about a computer language that has Unix-like roots has to start with showing the “Hello, world” program. So, here it is in Perl:

#!/usr/bin/perl
print "Hello, world!\n";

Let’s imagine that you’ve typed that into your text editor. (Don’t worry yet about what the parts mean and how they work. You’ll see about those in a moment.) You can generally save that program under any name you wish. Perl doesn’t require any special kind of filename or extension, and it’s better not to use an extension at all.[21] But some systems may require an extension like .plx (meaning PerL eXecutable); see your system’s release notes for more information.

You may also need to do something so that your system knows it’s an executable program (that is, a command). What you’ll do depends upon your system; maybe you won’t have to do anything more than save the program in a certain place. (Your current directory will generally be fine.) On Unix systems, you mark a program as being executable using the chmod command, perhaps like this:

$ chmod a+x my_program

The dollar sign (and space) at the start of the line represents the shell prompt, which will probably look different on your system. If you’re used to using chmod with a number like 755 instead of a symbolic parameter like a+x, that’s fine too, of course. Either way, it tells the system that this file is now a program.

Now you’re ready to run it:

$ ./my_program

The dot and slash at the start of this command mean to find the program in the current working directory. That’s not needed in all cases, but you should use it at the start of each command invocation until you fully understand what it’s doing.[22] If everything worked, it’s a miracle. More often, you’ll find that your program has a bug. Edit and try again—but you don’t need to use chmod each time, as that should “stick” to the file. (Of course, if the bug is that you didn’t use chmod correctly, you’ll probably get a “permission denied” message from your shell.)

There’s another way to write this simple program in Perl 5.10 or later, and we might as well get that out of the way right now. Instead of print, we use say, which does almost the same thing, but with less typing. It adds the newline for us, meaning that we can save some time forgetting to add it ourselves. Since it’s a new feature and you might not be using Perl 5.10 yet, we include a use 5.010 statement that tells Perl that we used new features:

#!/usr/bin/perl

use 5.010;

say "Hello World!";

This program only runs under Perl 5.10 or later. When we introduce Perl 5.10 or later features in this book, we’ll explicitly say they are new features in the text and include that use 5.010 statement to remind you. Perl actually thinks about the minor version as a three digit number, so ensure that you say use 5.010 and not use 5.10 (which Perl thinks is 5.100, a version we definitely don’t have yet!).

Typically, we only require the earliest version of Perl for the features that we need. This book covers up to Perl 5.14, so in many of the new features we preface the examples to remind you to add this line:

use 5.014;

What’s Inside That Program?

Like other “free-form” languages, Perl generally lets you use insignificant whitespace (like spaces, tabs, and newlines) at will to make your program easier to read. Most Perl programs use a fairly standard format, though, much like most of what we show here.[23] We strongly encourage you to properly indent your programs, as that makes your program easier to read; a good text editor will do most of the work for you. Good comments also make a program easier to read. In Perl, comments run from a pound sign (#) to the end of the line. (There are no “block comments” in Perl.[24]) We don’t use many comments in the programs in this book because the surrounding text explains their workings, but you should use comments as needed in your own programs.

So another way (a very strange way, it must be said) to write that same “Hello, world” program might be like this:

#!/usr/bin/perl
    print    # This is a comment
"Hello, world!\n"
;    # Don't write your Perl code like this!

That first line is actually a very special comment. On Unix systems,[25] if the very first two characters on the first line of a text file are #!, then what follows is the name of the program that actually executes the rest of the file. In this case, the program is stored in the file /usr/bin/perl.

This #! line is actually the least portable part of a Perl program because you’ll need to find out what goes there for each machine. Fortunately, it’s almost always either /usr/bin/perl or /usr/local/bin/perl. If that’s not it, you’ll have to find where your system is hiding perl, then use that path. On some Unix systems, you might use a shebang line that finds perl for you:

#!/usr/bin/env perl

If perl is not in any of the directories in your search path, you might have to ask your local system administrator or somebody using the same system as you. Beware though, that finds the first perl, which might not be the one that you wanted.

On non-Unix systems, it’s traditional (and even useful) to make the first line say #!perl. If nothing else, it tells your maintenance programmer as soon as he gets ready to fix it that it’s a Perl program.

If that #! line is wrong, you’ll generally get an error from your shell. This may be something unexpected, like “file not found” or “bad interpreter”. It’s not your program that’s not found, though; it’s that /usr/bin/perl wasn’t where it should have been. We’d make the message clearer if we could, but it’s not coming from Perl; it’s the shell that’s complaining.

Another problem you could have is that your system doesn’t support the #! line at all. In that case, your shell (or whatever your system uses) will probably try to run your program all by itself, with results that may disappoint or astonish you. If you can’t figure out what some strange error message is telling you, search for it in the perldiag documentation.

The “main” program consists of all the ordinary Perl statements (not including anything in subroutines, which you’ll see later). There’s no “main” routine, as there is in languages like C or Java. In fact, many programs don’t even have routines (in the form of subroutines).

There’s also no required variable declaration section, as there is in some other languages. If you’ve always had to declare your variables, you may be startled or unsettled by this at first. But it allows us to write quick-and-dirty Perl programs. If your program is only two lines long, you don’t want to have to use one of those lines just to declare your variables. If you really want to declare your variables, that’s a good thing; you’ll see how to do that in Chapter 4.

Most statements are an expression followed by a semicolon.[26] Here’s the one you’ve seen a few times so far:

print "Hello, world!\n";

As you may have guessed by now, this line prints the message Hello, world! At the end of that message is the shortcut \n, which is probably familiar to you if you’ve used another language like C, C++, or Java; it means a newline character. When that’s printed after the message, the print position drops down to the start of the next line, allowing the following shell prompt to appear on a line of its own, rather than being attached to the message. Every line of output should end with a newline character. We’ll see more about the newline shortcut and other so-called backslash escapes in the next chapter.

How Do I Compile My Perl Program?

Just run your Perl program. The perl interpreter compiles and runs your program in one user step:

$ perl my_program

When you run your program, Perl’s internal compiler first runs through your entire source, turning it into internal bytecodes, which is an internal data structure representing the program. Perl’s bytecode engine takes over and actually runs the bytecode. If there’s a syntax error on line 200, you’ll get that error message before you start running line 2.[27] If you have a loop that runs 5,000 times, it’s compiled just once; the actual loop can then run at top speed. And there’s no runtime penalty for using as many comments and as much whitespace as you need to make your program easy to understand. You can even use calculations involving only constants, and the result is a constant computed once as the program is beginning—not each time through a loop.

To be sure, this compilation does take time—it’s inefficient to have a voluminous Perl program that does one small quick task (out of many potential tasks, say) and then exits because the runtime for the program will be dwarfed by the compile time. But the compiler is very fast; normally the compilation will be a tiny percentage of the runtime.

An exception might be if you were writing a program run as a CGI script, where it may be called hundreds or thousands of times every minute. (This is a very high usage rate. If it were called a few hundreds or thousands of times per day, like most programs on the Web, we probably wouldn’t worry too much about it.) Many of these programs have very short runtimes, so the issue of recompilation may become significant. If this is an issue for you, you’ll want to find a way to keep your program in memory between invocations. The mod_perl extension to the Apache web server (http://perl.apache.org) or Perl modules like CGI::Fast can help you.

What if you could save the compiled bytecodes to avoid the overhead of compilation? Or, even better, what if you could turn the bytecodes into another language, like C, and then compile that? Well, both of these things are possible in some cases, but they probably won’t make most programs any easier to use, maintain, debug, or install, and they may even make your program slower.

A Whirlwind Tour of Perl

So, you want to see a real Perl program with some meat? (If you don’t, just play along for now.) Here you are:

#!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
    s/\w<([^>]+)>/\U$1/g;
    print;
}

Now, the first time you see Perl code like this, it can seem pretty strange. (In fact, every time you see Perl code like this, it can seem pretty strange.) But let’s take it line by line, and see what this example does. These explanations are very brief; this is a whirlwind tour, after all. We’ll see all of this program’s features in more detail during the rest of this book. You’re not really supposed to understand the whole thing until later.

The first line is the #! line, as you saw before. You might need to change that line for your system, as we showed you earlier.

The second line runs an external command, named within backquotes (` `). (The backquote key is often found next to the number 1 on full-sized American keyboards. Be sure not to confuse the backquote with the single quote, '.) The command we used is perldoc -u -f atan2; try typing that in your command line to see what its output looks like. The perldoc command is used on most systems to read and display the documentation for Perl and its associated extensions and utilities, so it should normally be available.[28] This command tells you something about the trigonometric function atan2; we’re using it here just as an example of an external command whose output we wish to process.

The output of that command in the backquotes is saved in an array variable called @lines. The next line of code starts a loop that will process each one of those lines. Inside the loop, the statements are indented. Although Perl doesn’t require this, good programmers do.

The first line inside the loop body is the scariest one; it says s/\w<([^>]+)>/\U$1/g;. Without going into too much detail, we’ll just say that this can change any line that has a special marker made with angle brackets (< >), and there should be at least one of those in the output of the perldoc command.

The next line, in a surprise move, prints out each (possibly modified) line. The resulting output should be similar to what perldoc -u -f atan2 would do on its own, but there will be a change where any of those markers appear.

Thus, in the span of a few lines, we’ve run another program, saved its output in memory, updated the memory items, and printed them out. This kind of program is a fairly common use of Perl, where one type of data is converted to another.

Exercises

Normally, each chapter will end with some exercises, with the answers in Appendix A. But you don’t need to write the programs needed to complete this section—those are supplied within the chapter text.

If you can’t get these exercises to work on your machine, double-check your work and then consult your local expert. Remember that you may need to tweak each program a little, as described in the text:

  1. [7] Type in the “Hello, world” program and get it to work! You may name it anything you wish, but a good name might be ex1-1, for simplicity, as it’s exercise 1 in Chapter 1. This is a program that even an experienced programmer would write, mostly to test the setup of a system. If you can run this program, your perl is working.

  2. [5] Type the command perldoc -u -f atan2 at a command prompt and note its output. If you can’t get that to work, find out from a local administrator or the documentation for your version of Perl about how to invoke perldoc or its equivalent. You’ll need this for the next exercise anyway.

  3. [6] Type in the second example program (from the previous section) and see what it prints. Hint: be careful to type those punctuation marks exactly as shown! Do you see how it changed the output of the command?



[4] To be sure, the first edition was written by Randal L. Schwartz, the second by Randal and Tom Christiansen, then one by Randal and Tom Phoenix, and now three by Randal, Tom Phoenix, and brian d foy. So, whenever we say “we” in this edition, we mean that last group. Now, if you’re wondering how we can say that we’ve enjoyed writing it (in the past tense) when we’re still on the first page, that’s easy: we started at the end, and worked our way backward. It sounds like a strange way to do it, we know. But, honestly, once we finished writing the index, the rest was hardly any trouble at all.

[5] Except on Tuesdays, during a power outage, when you hold your elbow at a funny angle during the equinox, or when use integer is in effect inside a loop block being called by a prototyped subroutine prior to Perl version 5.12.

[6] We even discussed doing the entire book as a footnote to save the page count, but footnotes on footnotes started to get a bit crazy.

[7] Not all at once.

[8] We’re not insulting Larry by saying he’s lazy; laziness is a virtue. The wheelbarrow was invented by someone who was too lazy to carry things; writing was invented by someone who was too lazy to memorize; Perl was invented by someone who was too lazy to get the job done without inventing a whole new computer language.

[9] Don’t worry if you don’t know what these are. All that matters is that they were the programs Larry had in his Unix toolbox, but they weren’t up to the tasks at hand.

[10] But we hope you’ll crash less often with the car.

[11] If you’re going to use a programming language for only a few minutes each week or month, you’d prefer one that is easier to learn, since you’ll have forgotten nearly all of it from one use to the next. Perl is for people who are programmers for at least twenty minutes per day, and probably most of that in Perl.

[12] We won’t explain it all here, but this example pulls some data from an input file or files in one format and writes some of it out in another format. All of its features are covered in this book.

[13] With a sharp jump when any one section of the program exceeds the size of your screen.

[14] Don’t just take our word for it, though. If you want to know whether Perl is better than language X, learn them both and try them both, then see which one you use most often. That’s the one that’s best for you. In the end, you’ll understand Perl better because of your study of language X, and vice versa, so it will be time well spent.

[15] If system administrators can’t install software, what good are they? If you have trouble convincing your admin to install Perl, offer to buy a pizza. We’ve never met a sysadmin who could say no to a free pizza, or at least counteroffer with something just as easy to get.

[16] And no, as we write this, it won’t fit in your Blackberry—it’s just too darn big, even stripped down. We’ve heard rumors that it runs on WinCE though.

[17] It’s nearly always better to compile Perl from the source on Unix systems. Other systems may not have a C compiler and other tools needed for compilation, so CPAN has binaries for these.

[18] Many mailing lists are listed at http://lists.perl.org.

[19] Even Larry admits to consulting the documentation from time to time.

[20] Maybe even two or three times. Many times, we’ve gone into the documentation looking to explain a particular unexpected behavior and found some new little nuance that ends up on a slide or in a magazine article.

[21] Why is it better to have no extension? Imagine that you’ve written a program to calculate bowling scores and you’ve told all of your friends that it’s called bowling.plx. One day you decide to rewrite it in C. Do you still call it by the same name, implying that it’s still written in Perl? Or do you tell everyone that it has a new name? (And don’t call it bowling.c, please!) The answer is that it’s none of their business what language it’s written in, if they’re merely using it. So it should have simply been called bowling in the first place.

[22] In short, it’s preventing your shell from running another program (or shell built-in) of the same name. A common mistake among beginners is to name their first program test. Many systems already have a program (or shell built-in) with that name; that’s what the beginners run instead of their program.

[23] There is some general advice (not rules!) in the perlstyle documentation.

[24] But there are a number of ways to fake them. See the perlfaq portions of the documentation.

[25] Most modern ones, anyway. The “sh-bang” mechanism, pronounced “sheh-bang” as in “the whole shebang”, was introduced somewhere in the mid-1980s, and that’s pretty ancient, even on the extensively long Unix timeline.

[26] You only need semicolons to separate statements, not terminate them.

[27] Unless line 2 happens to be a compile-time operation, like a BEGIN block or a use invocation.

[28] If perldoc is not available, that probably means that your system doesn’t have a command-line interface, and your Perl can’t run commands (like perldoc!) in backquotes or via a piped open, which you’ll see in Chapter 14. In that case, you should simply skip the exercises that use perldoc.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required