Quick Reference

by Mícheál Ó Foghlú

C  O  N  T  E  N  T  S


Perl Overview

Special Variables



Regular Expressions

Reference Tables



Perl Quick Reference

Copyright © 1996 by Que Corporation.

All rights reserved. Printed in the United States of America. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 W. 103rd Street, Indianapolis, IN, 46290. You may reach Que's direct sales line by calling 1-800-428-5331.

ISBN: 0-7897-0888-4

HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website :
    e-mail :


PresidentRoland Elgey PublisherJoseph B. Wikert
Publishing Manager Fred Slone Senior Title Manager Bryan Gambrel
Editorial Services Director Elizabeth Keaffaber Managing Editor Sandy Doell
Director of Marketing Lynn E. Zingraf Acquisitions Editor Al Valvano
Production Editor Patrick Kanouse EditorsElizabeth Barrett, Jeff Riley
Product Marketing Manager Kim Margolius Assistant Product Marketing Manager Christy M. Miller
Strategic Marketing Manager Barry Pruett Technical Editor Joe Milton
Technical Support Specialist Nadeem Muhammed Acquisitions Coordinator Carmen Krikorian
Software Relations Coordinator Patty Brooks Editorial Assistant Andrea Duvall
Book Designer Ruth HarveyCover Designer Nathan Clement
Production Team Marcia Brizendine, Jenny Earhart, Joan Evan, DiMonique Ford, Amy Gornik, Staci Somers
IndexerBecky Hornyak

This book is dedicated to my mother, Máire Foley, who bought me my first computer (and didn't laugh too hard when the library system I developed could only hold details on three books); and also to my father, Michael John Pats Phaddy Amhlaoibh Ó Foghlú, who taught me the value of books and the importance of names.

About the Author

Mícheál Ó Foghlú is now a lecturer in Applied Computing and Information Systems in Waterford Regional Technical College, Ireland ( Until September 1996, he was working in the Computer Services department in University College Galway, Ireland ( His interests include Natural Language Processing, WWW programming and development, Linux, computing using the Irish language, and Z39.50. When not slaving over a hot computer, he is sometimes seen nursing a quiet pint while listening to loud Irish music, or meandering through the hills in no particular direction.

He can be contacted


I would like to thank all my colleagues in the Computer Services department in UCG, especially Paul Doyle and Sinéad N' Fhaoláin, for bearing with me. Thanks are also due to all the Que team for their cooperation and help in the production of this book. Though I do not know him personally, I would like to thank Larry Wall for contributing Perl to the rest of us!

We'd Like to Hear from You!

As part of our continuing effort to produce books of the highest possible quality, Que would like to hear your comments. To stay competitive, we really want you, as a computer book reader and user, to let us know what you like or dislike most about this book or other Que products.

You can mail comments, ideas, or suggestions for improving future editions to the address below, or send us a fax at (317) 581-4663. For the online inclined, Macmillan Computer Publishing has a forum on CompuServe (type GO QUEBOOKS at any prompt) through which our staff and authors are available for questions and comments. The address of our Internet site is (World Wide Web).

In addition to exploring our forum, please feel free to contact me personally to discuss your opinions of this book: I'm 74670,3710 on CompuServe, and I'm on the Internet.

Thanks in advance-your comments will help us to continue publishing the best books available on computer topics in today's market.

Al Valvano
Product Development Specialist
Que Corporation
201 W. 103rd. Street
Indianapolis, Indiana 46290


This book is a reference guide for the programming language called Perl. This book does not describe how to install Perl on your computer; if you do not already have Perl installed, this book will not be very useful!

If you want to install Perl and have access to the Internet visit the Central Perl Archive Network (CPAN). The master site is at, and there are many mirror sites around the world. This is as much as can be found on Perl installation in this guide!

Perl has many uses, especially in UNIX system administrative tasks, which is where Perl was born and grew up. The name stands for "Practical Extraction and Report Language." Nowadays, Perl is seen by many as the ideal development language for Web server scripts.

This chapter describes the advantages of using Perl and outlines the structure of Perl Quick Reference.

Why Use Perl?

People use Perl because it is quick, efficient, and easy to maintain when programming a wide range of tasks, in particular those involving the manipulation of text files. Also, there are many others also using Perl who are prepared to share their code.

Rapid Development

Many programming projects are high level rather than low level. That means that they tend not to involve bit-level manipulations, direct operating system calls. Instead, they focus on reading from files, reformatting the output, and writing it to standard output-for example, a Web browser. With Perl, the programmer does not need to get involved in the details of how file handles and buffers are manipulated, how memory is allocated, and so on. You can tell it to slurp in the contents of a file and display it on the standard output device but with all newlines replaced by tabs:

while ( <INFILE> )  {  s/\n/\t/;  print;  }

Let's not worry about the details of what's happening in that code example until the "Perl Overview" chapter. Just notice two things:

In a nutshell, that's the secret to rapid development: Write small amounts of powerful code without having to pause to consider awkward issues of syntax at every step.

Perl is pithy; a little Perl code goes a long way. In terms of programming languages, that usually means that the code will be difficult to read and painful to write. But although Larry Wall, the author of Perl, says that Perl is functional rather than elegant, most programmers quickly find that Perl code is very readable and that it is not difficult to become fluent at writing it. This is especially true of the high-level, macro operations typically required in Web development.

As it happens, Perl is quite capable of handling some pretty low-level operations, too. It can handle operating system signals and talk to network sockets, for example.

Compiler and Interpreter

A program by itself can't achieve anything. To carry out its work, it needs to be fed to either a compiler or an interpreter. Both have their advantages:

There are advantages and disadvantages to both approaches. Compiled code takes longer to prepare, but then it runs fast and your source stays secret. Interpreted code gets up and running quickly but isn't as fast as interpreted code. You also need to distribute the program source code if you want to allow others to run your programs.

So which of these categories describes Perl?

Well, Perl is a little special in this regard; it is a compiler that thinks it's an interpreter. Perl compiles program code into executable code before running it, so there is an optimization stage and the executable code runs quickly. However, it doesn't write this code to a separate executable file. Instead, it stores it in memory and then executes it.

This means that Perl combines the rapid development cycle of an interpreted language with the efficient execution of compiled code. The corresponding disadvantages are also there, though: The need to compile the program each time it runs means a slower startup than a purely compiled language and requires developers to distribute source code to users.

In practice, these disadvantages are not too limiting. The compilation phase is extremely fast, so you're unlikely to notice much of a lag between invoking a Perl script and the start of execution.

In summary, Perl is compiled "behind the scenes" for rapid execution, but you can treat it as if it is interpreted. This makes it easy for you to tweak your HTML; just edit the code and let the users run it. But is that good programming practice? Hey, that's one for the philosophers.


Perl was not designed in the abstract. It was written to solve a particular problem and it evolved to serve an ever widening set of real-world problems.

It could have been expanded to handle these tasks by adding more and more keywords and operators, hence making the language bigger. Instead, the core of the Perl language started out small and became more refined as time went on. In some ways, it actually contracted; the number of reserved words in Perl 5 is actually less than half the number in Perl 4, not more.

This reflects an awareness that Perl's power lies in it's unique combination of efficiency and flexibility. Perl itself has grown slowly and thoughtfully, usually in ways that allow for enhancements and extensions to be added on rather than being hard-wired in. This approach has been critical in the development of Perl's extensibility over time, as the next section explains.


Much of the growth in Perl as a platform has come by way of the increasing use of libraries (Perl 4) and modules (Perl 5). These are mechanisms that allow developers to write self-contained portions of Perl code that can be slotted in to a Perl application.

These add-ons range from fairly high-level utilities, such as a module that adds HTML tags to text, to low-level, down-and-dirty development tools such as code profilers and debuggers.

The ability to use extensions like these is a remarkable advance in the development of a fairly slick language, and it has helped to fuel the growth in Perl use. It makes it easy for Perl developers to share their work with others; the arrival of objects in Perl 5 makes structured design methodologies possible for Perl applications. The language has come of age without loosing any of its flexibility or raw power.

Web Server Scripts

Web servers generate huge amounts of HTML. The "M" stands for "Markup," and you need lots of it to make your Web pages look more exciting than the average insurance contract. It's an awkward business though, with problems arising easily if tags are misplaced or misspelled. Perl is a good choice of language to look after the details for you while you get on with the big picture. This is especially true if you call on Perl 5's object-oriented capabilities.

Another facet of Perl that is of particular interest to many Web server managers is that Perl works very well with standard UNIX DBM files and support for proprietary databases is growing. This is a significant consideration if you plan to allow users to query database material over the Web.


Security is a major issue when writing system administrative programs and on the Internet in general. Using Perl for scripting on your Web server, you can easily guard against users trying to sneak commands through for the server to execute on their behalf. There is also an excellent Perl 5 module called pgpperl, also known as Penguin, that allows your server to use public-key cryptography techniques to guard sensitive data from eavesdroppers.


Lots of people on the Web already use Perl! Going with the flow isn't always the best approach, but Perl has grown with the Web. There is a lot of experience out there if you need advice. The Perl developers are keenly aware of Web issues as they add to Perl. And many Perl modules have been built with the Web specially in mind.

Why You Want to Use Perl

There are many reasons why you want to use Perl. It is small, efficient, flexible, and robust. Perl is particularly well suited for Web development work where text output is a major preoccupation. If the reasons previously outlined aren't quite enough, consider this: Perl is completely free.

The Structure of This Book

This book falls clearly into two parts. Part 1 is a discursive overview of the Perl language, describing some things that are not easily summarized in tables. Part 2 is a set of reference chapters that describes the various aspects of Perl in detail.