Sams Teach Yourself Laura Lemay's Teach Yourself PERL in 21 Days

Sams Teach Yourself Laura Lemay's Teach Yourself PERL in 21 Days

by Laura Lemay
Teach Yourself Perl in 21 Days will cover the basics in the first few chapters, and then move to practical uses of Perl and in-depth discussions of more advanced topics. Later chapters will also delve into software engineering topics, with discussions of modular code and object-oriented programming. CGI will be covered in one chapter, but will not be the focus of the


Teach Yourself Perl in 21 Days will cover the basics in the first few chapters, and then move to practical uses of Perl and in-depth discussions of more advanced topics. Later chapters will also delve into software engineering topics, with discussions of modular code and object-oriented programming. CGI will be covered in one chapter, but will not be the focus of the book. Teach Yourself Perl will rely heavily on longer working examples and code, as opposed to small snippets and code fragments. Each chapter will include two to three smaller complete examples and one major one that illustrates most of the concepts for that chapter and builds on the chapters before it.

  • Provides step-by-step lessons for one of the most popular languages used in Web programming
  • Written in a straightforward, easy-to-understand style with numerous useful and complete code examples
  • Laura Lemay s third major book the follow-on to her Teach Yourself Java and Teach Yourself Web Publishing with HTML

Product Details

Publication date:
Sams Teach Yourself Series
Edition description:
Older Edition
Product dimensions:
7.39(w) x 9.12(h) x 1.66(d)

Read an Excerpt

Day 1: An Introduction to Perl

Greetings and welcome to Sams Teach Yourself Perl in 21 Days! Today, and for the next 20 days, I'll be your guide to all things Perlish. By the time you finish this book-or at least by the time you put it down-you should know enough Pert to accomplish just about anything you'd like to do and to converse intelligently with other Pert programmers.

Today we're going to start with some basic information to ease you into working with Pert. Today's lesson is kind of short, just enough to give you a little background and to get you started using Pert with some simple scripts. In particular, you'll learn

  • Some background on Pert: what it is, who created it, who's working on it now.

  • Why you should learn Pert (reasons besides "my boss told me to").

  • Some actual Pert code, so you can get an idea for how Pert is written and run (and be prepared for the rest of this week's lessons).

And so, without further ado, let's get started. Week one, chapter one, section one-Onward!

What Is Perl and Where Did It Come From?

Perl is not a typo for pearl. Perl is actually an acronym. It stands for Practical Extraction and Report Language, which is a pretty good description of what Perl does particularly well: Extraction for looking at files and pulling out the important parts (for example, the actual data from an HTML file, or the user or host names from a networking log file); and report for generating output and, well, reports, based on the information that was found. It's a practical language because it's much easier to write these sorts of programs in Perl than it would be in a language such as C. But no one really gets hung up on the acronym any more; Perl is just Perl.

Perl was created in 1987 by Larry Wall, who at the time was already well known in the UNIX software world for having written the freely available patch program as well as the Usenet newsreader rn. The story goes that Larry was working on a task for which the UNIX program awk (the popular extraction and report language of the time) wasn't powerful enough, but he realized the task would take a whole lot of work to accomplish in a language such as C. So Perl was created as a scripting language that borrows bits from various other UNIX tools and languages, such as sed, grep, awk, shell scripting and, yes, C. Also, as with patch and rn, Perl was released for free to the UNIX community. And the UNIX community said it was good.

For many years, Perl was the language of choice for UNIX system administrators and other UNIX programmers who needed a flexible, quick-to-prograrn language to accomplish tasks for which a language such as C would be overkill (or take too much work), or for tasks that were too complex for shell scripting. It was because of its existing popularity as a UNIX language that Perl became popular as a Web language for creating CGI scripts-most early Web servers ran on UNIX systems. CGI (Common Gateway Interface) allowed programs and scripts to be run on the Web server in response to forms or other input from Web pages. Perl fit neatly into that niche, and in the last couple of years as the Web and CGI have become more and more popular, the interest in the Perl language has also grown dramatically. Even as Web servers have ended up being run on systems other than UNIX, Perl moved with them and continued to be popular in this realm.

Larry Wall originally "owned" Perl and was responsible with keeping it updated and incorporating new changes. With Perl's recent surge in popularity, however, the task of maintaining Perl now falls to a close-knit group of volunteer programmers. These programmers, including Larry Wall, maintain the core Perl source, port the source to platforms other than UNIX, coordinate bug fixes, and establish the "standard" Perl release (the Changes file in the standard Perl distribution lists the primary dramatis personae of Perl). No single organization owns Perl; like the GNU tools (GNU emacs, GCC, GDB, and so on) and the Linux operating system, Perl is defined and managed on a volunteer, goodwill basis. It's also available free of charge; all you have to do is download and install it.

The current major version of Perl is Perl 5, and it is the version this book covers. All Perl releases also have minor versions with various numbers and letters, which most commonly fix bugs that were found in the original release (although some new features do creep into the minor version releases). The current UNIX and Windows version of Perl as I write this book is 5.005, with the Macintosh version at 5.004. These minor numbers may have changed by the time you read this, as work is ongoing on all the Perl ports (and at any time, there may be separate releases for less stable beta versions as well as the current "official" version). For the most part, however, the minor version is less important than making sure you have some version of Perl 5 installed.

Note: As of Perl 5.005, support for Windows was folded into the core source code Note: The examples in this book were written on the UNIX version of Perl, version

Why Learn Perl?

There are an enormous number of programming languages available on the market today, all of which seem to claim to be able to solve all your problems in half the time, at a quarter of the cost, and then bring about world peace, too. So why learn Perl over another one of those vaunted languages?

The best reason is that different tools are best for different tasks, and Perl is particularly good at a number of common tasks. But there are a number of other good reasons to learn and use Perl.

Perl Is Useful

How's that for a good reason? Perl is probably the language for you if any of the follow- ing profiles describes you.

  • You're a system administrator looking for a general-purpose scripting language.

  • You're a Web administrator with a dozen CGI programs that your Web designers want you to write.

  • You're a fairly well-versed UNIX user looking to expand your knowledge.

  • You're a programmer looking to quickly prototype a more complicated program.

  • You just want a language that will let you hack around with stuff. You can do real work in Perl, and you can get it done right away.

Perl Doesn't Need Any Fancy Software

To use Perl, you do not need to buy a nifty shrink-wrapped Perl program. You don't need a Perl compiler or an integrated Perl development environment. You don't need a browser that supports Perl or a computer that says "Perl Inside." All you need is one thing: the standard Perl interpreter. You can get that for free, simply by downloading it. If you've got a UNIX shell account with an ISP, you've probably already got it.

OK, there is one other thing you need: You must have a text editor in which to write your Perl scripts. One of these comes with just about every system you can run Perl on, so you're still safe.

You Can Program Perl Quickly

Perl is a scripting language, which means that your Perl scripts are just text files, and they're executed on the fly when Perl runs them. You don't have to use a compiler to convert your code to some other format like an executable or a bytecode file, as you would for a language such as C or Java. This makes Perl quicker to get running for inital programs, makes the debugging process faster, and also allows you to modify your Carl scripts quicker and easier than in C.

Note: Scripting languages are often called interpreted languages in Computer scripts right away as you can with an interpreted language, it also gives you some measure of control over the compilation process. Perl also runs faster than a pure interpreted language does (although not as fast as a compiled language).

Perl Is Portable

Because Perl is a scripting language, a Perl script is a Perl script is a Perl script regardless of which platform you run it on. Although there are differences in Perl on different platforms and features that work only on some platforms (I'll point those out as we run across them), in many cases moving a script from one platform to another can be done without modifying the script in any way whatsoever-without having to laboriously port to a new operating system, without even having to recompile your source code.

Perl Is Powerful

Perl was designed to be a superset of many fairly complex UNIX tools. It's also got all the features you'd expect in a high-level language (and many you wouldn't). Most everything you can do in a sophisticated language such as C, you can do in Perl, although there are, of course, tasks for which C would be better than Perl, and vice versa. You can do simple top-to-bottom scripts in Perl. You can do structured programming in Perl. You can do advanced data structures in Perl. You can do object-oriented programming in Perl. It's all there.

If Perl alone isn't good enough for you, there are also extensive archives of various tools and libraries (called modules) to do various common tasks in Perl. Modules to do database interaction, networking, encryption, ties to other languages-just about everything you can think of-are available in these archives. In many cases, if you think, "I need to do X in Perl"' someone has already done X and put their code in the archives for you to use. Perl's cooperative nature means that there is an enormous amount of resources for you to take advantage of.

Perl Is Flexible

One of the mottoes of Perl is: "There's more than one way to do it." (This is commonly referred to by Perl folk as the acronym TMTOWTDI). Remember this phrase, because IT be coming back to it throughout the book. The Perl language was designed around the idea that different programmers have different ways of approaching and solving problems. So instead of you having to learn how to adapt your thinking to a small set of commands and syntactical constructs (as in C or Java), the Perl language has an enormous number of constructs and shortcuts and side effects-many of which accomplish the same thing other constructs do, but in slightly different ways.

This does make Perl a very large, complex, complicated language (when I first encountered Perl as a young and idealistic programmer, my first reaction was "It's so UGLY!"). But Perl's size and depth of features also make Perl extremely flexible and fun to use. You can write a very straightforward, C-like Perl script-one that uses some of Perl's various shortcuts, while still being entire readable-or one that relies so heavily on Perl side effects and hidden features that you can make other programmers' heads explode just trying to read it (sometimes that can be a fun way to spend a quiet afternoon). It's your choice how you want to use Perl-you don't have to modify your thinking to fit the language. (These days, I still occasionally think Perl is ugly-but I also think it's really cool and fun to work in. I can't say that about C, which is a more consistent and elegant language, but seems to involve a lot more drudgery)....

Meet the Author

Laura Lemay is one of the world's most popular authors on Web development topics. She is the author of the best-selling Sams Teach Yourself Java in 21 Days and Sams Teach Yourself Web Publishing with HTML in a Week. She is known for her ability to clearly explain even the most difficult technical topics, while complementing her discussions with interesting yet practical examples.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >