Perl is short for " Practical Extraction and Report Language," although it has also been called a "Pathologically Eclectic Rubbish Lister," among other expansions.[11] There's no point in arguing which expansion is correct, because both of those are endorsed by Larry Wall, Perl's creator and chief architect, implementor, and maintainer. He 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,[12] 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.
[11]It's actually a retronym, not an acronym. That is, Larry came up with the name first, and the expansion later. That's why "Perl" isn't in all caps.
[12]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.
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,[13] without having to resort to a language like C.
[13]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.
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, 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 that, 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.[14]
[14]But we hope you'll crash less often with the car.
Perl is nearly unlimited. There are very few things you can't do with Perl. You wouldn't want to write a 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 which 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 sister to this one. 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.
It's easy to use, but sometimes hard to learn. This is a generalization, of course. But in designing Perl, Larry has had to make 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.[15]
[15]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.
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:[16]
[16]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.
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.
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 30% to 70% 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[17] (rather than being proportional to the program's functionality), the shorter source in Perl will mean fewer bugs on average.
[17]With a sharp jump when any one section of the program exceeds the size of your screen.
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 line-noise to the uninitiated, but to the seasoned Perl programmer, it looks like checksummed line-noise with a mission in life. 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.[18]
[18]An actual annual event sponsored by the Perl Journal (at http://www.tpj.com/ ).
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 has now grown to have 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.
Larry is still in charge of Perl, although the Perl development team is now made up of approximately thirty key people and a few hundred others from around the world. And Perl is still growing.
These days, Perl is still free for you to use. In fact, Larry promises that it will always be free. (He's a really nice guy; you'd like him.) So go ahead and write code in Perl today, without worrying that there will be a licensing fee on your program tomorrow.
So, if Perl is free, who pays Larry and the other Perl developers? Well, the majority of us contribute to Perl as a labor of love; Perl helps us, and we help Perl. (If you ever see some way in which you could improve Perl, we encourage you to send in your contributions, too.) In some cases, though, a person or firm has paid someone to do some development work. This may be because they needed some new functionality badly enough to pay for it, or because they wanted to make the world a better place.
Larry doesn't write all of the code these days, but he still guides the development and makes the big decisions. One of the most important rules he's given us is this one: "Common things should be easy; advanced things should at least be possible."
Because of that rule, you can be sure that anything that you need to do frequently will have a shortcut in Perl. In fact, by the end of this book, you'll probably be using at least ten shortcuts in a typical ten-line program. That is the sort of thing that makes Perl easier to use, at the price of being harder to learn.
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 could know every language; you'd always be able to choose the best language for each project. Most of the time, you'd choose Perl.[19]
[19]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.
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 lots of content to be moved 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 other than for CGI?" We find those statements amusing.
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 to 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."
If you think you really want to compile your Perl code to make a binary, though, see Section 1.4.3, "But How Do I Compile Perl?" later in this chapter.
Copyright © 2002 O'Reilly & Associates. All rights reserved.