Read an Excerpt
This chapter covers the fundamental Perl skills that we'll rely on in the coming chapters. In the next few chapters, we're going to see a great amount of Perl syntax at work, but none of that is any use unless you can get Perl running and create programs with it. That fundamental set of skills-creating and running Perl programs-is the topic of this chapter.
Here, we're going to work through the mechanics of creating Perl programs, from installation issues to writing Perl code, from making sure your Perl program can find the Perl installation on your machine to displaying simple output. These skills are ones we'll need in the coming chapters. The material in those chapters is all about the internals of writing Perl code; this chapter is all about the rest of the process that makes that code run.
You're likely to know much of the material in this chapter, in which case it will provide a review. (Some of the material is bound to be new; off the top of their heads, for instance, very few people know what all the Perl command-line switches do.) If you already have a working installation of Perl, and can write and run basic Perl programs, you're already familiar with most of what you'll see in this chapter, so just skim the following pages and continue with Chapter 2.
This book is written to be as comprehensive on the subject of Perl as one book can be, and covering the basics of creating and running Perl programs is essential information.
All About Perl
Taking a look at the history of Perl is instructive: If you know how Perl grew up, you know why it concentrates (sometimes unexpectedly) on various programming areas and can be thin in others. To the uninitiated, the structure of Perl can seem a little haphazard at times (for example, why does it expend so much effort on regular expressions to handle string matching?). Unraveling its history helps unravel those mysteries.
Perl was created in 1986, as a tool to track system resources across a network (called a configuration manager tool). To set the record straight, Perl is an interpreted language that was originally designed to scan text files, extract information from those text files, and display text-based reports using that information. In other words, it was built to handle, process, and format text. In fact, the name itself stands for Practical Extraction and Reporting Language (also affectionately known as the Pathologically Eclectic Rubbish Lister, as well as many other names, like the Swiss Army Chainsaw, which refers to the fact that you can do nearly everything in Perl in many different ways, and some of them are pretty forceful).
TIP: You might ask: Why "Perl" and not "Pearl"? It turns out that a graphics language called Pearl already existed when Perl was created; even so, note that the actual acronym of the Practical Extraction and Reporting Language is, in fact, Pearl if you include all the words.
An interpreted language like Perl is parsed and executed at runtime instead of being compiled into binary form and then run (although a Perl compiler is being developed). That is, you use the Perl interpreter named perl (note the case difference) to run Perl programs. And note that although I've been using the generic term program up to this point, it's more correct to refer to Perl code as scripts, as with other interpreted languages.
With other languages, such as C++, you create a binary executable and then run it. Using an interpreted language has advantages and disadvantages; the primary advantage is that it cuts time you invest in the development/testing cycle. Instead of having to run your code through a compiler each time you want to test it, you can run it immediately using the Perl interpreter. On the other hand, compiled programs almost always run faster and don't need an interpreter installed on the machine in which they run (you'll need a Perl installation on the machine in which you want to run Perl programs). However, the typical Perl program today is not a long one-usually a CGI script a few pages in length or code that acts as glue binding together operating system shell commands-so execution time is often not a significant factor.
It's worth noting that, increasingly, people are writing longer Perl scripts because Perl has evolved into a cross-platform language that is the center of a thriving cybercommunity. Perl was born in Unix, but it has spread to just about every operating system out there, and it's substantially the same on all of them. Because you don't need to create binary executables, you can easily port your scripts to all those operating systems. In fact, it's no longer possible today to write serious Perl books under the assumption that all readers are Unix programmers; although many books make this mistake, that's living in the past. Perl today is a cross-platform language and must be treated that way, not as limited to one particular operating system (and how much help is it to a Windows programmer to describe something as awk(1)-like or to say it works just like sed(1)? Better to explain such things in ways everyone concerned can understand).
Some people wonder at the popularity of Perl-a text-based language designed to be run from the command-line-in a world of graphical user interfaces like Windows. Perl's continued and growing popularity is based on a number of factors: First, of course, is that many operating systems remain primarily text-oriented. Another is that Perl is a cross-platform language, supported on many different operating systems to a remarkable degree, differing across platforms only in some unavoidable ways (such as the number of bytes used to store long integers in the host computer).
In addition, Perl actually has become graphical by interfacing with the popular Tk.pm module (as we'll see in Chapter 16), allowing it to use the popular graphical controls (called widgets) in the Tcl language's Tk toolkit. Using the Tk.pm module, you can display windows with buttons, menus, and more from Perl.
However, on the basis of a sheer number of programmers, Perl's recent popularity has been fed most these days by Common Gateway Interface (CGI) programming, which you use to perform Web-based client/server operations. It's no drawback to use a text-based language when you're creating Web pages, which are themselves text-based. CGI programming in Perl is very powerful and-correspondingly-it's one of the major topics we'll cover.
That's enough introduction. It's time to start creating Perl scripts and seeing what goes into the process.
Getting And Installing Perl
The big boss gives you a call-as usual, at the last minute. You have 20 minutes to write a new application that will convert text files between various formats. What are you going to do? Knowing how well Perl works with files and handles text, you select Perl as the language of choice to get the task done. Of course, you've got to make sure you have it before you can use it.
Perl is free; all you have to do is download it and install it. If you're on a multiuser computer, you may already have Perl installed; try entering the following at the command-line (I'll use % for the command-line prompt in this book):
Here, I'm using the -v switch (a switch is a command to the Perl interpreter, and it starts with a hyphen,-). If Perl is installed and in your path, this command will display the current Perl version and patch level. (Perl patches are periodically released to fix individual bugs.)
Note that on some systems, the default Perl interpreter is an earlier version of Perl, such as version 4. To use Perl 5 on such a system, you use a command such as perl5 (try the following if perl -v indicates a version of Perl before version 5):
If you don't have Perl installed, go to www.perl.com (Europeans may prefer a European mirror such as www.cs.ruu.nl) or www.cpan.org. CPAN is the Comprehensive Perl Archive Network, an extensive resource that we'll see more about throughout this book. From those sites, you can find and download all that you need.
We're (intentionally) not going to cover the installation techniques you use on various operating systems to install Perl. Not only are those techniques detailed very carefully and well on the Perl site (such as the Unix installation tutorial at www.perl.com/CPAN-local/doc/relinfo/INSTALL.html), but they are also subject to future changes that would not be reflected in this book. (Many books have made themselves obsolete by giving detailed installation instructions, such as those for the Java language-which then changed almost immediately with newly released versions.)
As of this writing, you can get the latest version of Perl most easily by clicking on the "Get the latest version of Perl" link at www.perl.com. This link connects you to www.perl.com/pace/pub/perldocs/latest.html, giving you direct links to the most popular ports (that is, system-specific implementations) of Perl, such as ActiveState's Perl for Win32 (make sure you get version 5.005 or later to ensure your Perl for Win32 is compatible with Unix-based Perl and Perl modules-earlier versions had some incompatibilities), the Macintosh port named MacPerl, and the many Unix ports. What types of Unix is Perl supported under? Following is the short list:
- AIX 3, 4
- BSD/386 1
- ConvexOS 10
- DG/UX 5
- Digital Unix/DEC OSF/1 1, 2, 3, 4
- Free/Open/Net BSD
- HPUX 9, 10
- Interactive 3
- IRIX 4, 5, 6
- Linux 1, 2
- MachTen 2, 4
- NextStep 3, 4
- SCO 3
- SunOS 4, 5
- Ultrix 4
- UNICOS 6, 7, 8, 9
Perl is supported under nearly every type of Unix available; in fact, as far as I'm aware, the only types of Unix that it's not supported under are very early versions, such as those for some PDP-11s that don't have enough resources available.
The usual starting point to get Perl is www.perl.com, and the latest version page, www.perl.com/pace/pub/perldocs/latest.html. Download the Perl port that makes sense for you, and follow the installation instructions to install Perl.
Besides www.perl.com, you can also get anything Perl-related at CPAN. CPAN is a practically endless source of Perl modules, packages, utilities, ports, and more. The creators of CPAN describe it this way: "The CPAN contains the collected wisdom of the entire Perl community: hundreds of Perl utilities, several books' worth of documentation, and the entire Perl distribution. If it's written in Perl, and it's helpful and free, it's in the CPAN."
To get to CPAN, go to www.cpan.org. You can automatically be connected to the CPAN mirror nearest you if you navigate to www.perl.com/CPAN/ (the case in this URL is important). If you omit the final slash-that is, www.perl.com/CPAN-you'll see a list of the CPAN mirror sites and can select one you want.