Preface

The first edition of CGI Programming on the World Wide Web was published in early 1996. The Web was very different then: the number of web hosts was 100,000, Netscape Navigator 2.0 (the first JavaScript™-enabled browser) was released, and Java™ was less than a year old and was used primarily for applets. The Web was still young, but it was developing quickly.

In 1996, CGI was the only stable and well-understood method for creating dynamic content on the Web. However, very few sites exploited its full potential. In the first edition, Shishir wrote:

Today’s computer users expect custom answers to particular questions. Gone are the days when people were satisfied by the computing center staff passing out a single, general report to all users. Instead, each salesperson, manager, and engineer wants to enter specific queries and get up-to-date responses. And if a single computer can do that, why not the Web?

This is the promise of CGI. You can display sales figures for particular products month by month, as requested by your staff, using beautiful pie charts or plots. You can let customers enter keywords in order to find information on your products.

In 1996, these were bold claims. Today, they describe business as usual. That promise of CGI has certainly been fulfilled.

This book is about more than writing CGI scripts. It is about programming for the Web. Although we focus on CGI programming with Perl (thus the title change for this edition), many of the concepts we cover are common to all server-side web development. Even if you find yourself working with alternative technologies down the road, the effort you invest learning CGI now will continue to yield value later.

What’s in the Book

Because CGI has changed so much in the last few years, it is only appropriate that this new edition reflect the changes. Thus, most of this book has been rewritten. New topics include CGI.pm, HTML templates, security, JavaScript, XML, search engines, style suggestions, and compatible, high-performance alternatives to CGI. Previous topics, such as session management, email, dynamic images, and relational databases, have been expanded and updated. Finally, we modified our presentation of CGI to begin with a discussion of HTTP, the underlying language of the Web. An understanding of HTTP provides a foundation for a more thorough understanding of CGI.

Despite the changes, the original goal of this book remains the same: to teach you everything you need to know to become a good CGI developer. This is not a learn-by-example book—it isn’t built around a handful of CGI scripts with a discussion of how each script works. There are already lots of books like that available for CGI. While these books can certainly be useful, especially if one of the examples matches a particular challenge you are facing, they often teach how without explaining why. The aim of this book is to cover the fundamentals so that you can create CGI scripts to tackle any challenge. Don’t worry, though, because we’ll look at lots of examples. But our examples will serve to illustrate the discussion, rather than the other way around.

We should admit up front that there is a Unix bias in this book. Both Perl and CGI were originally conceived for the Unix platform, so it remains the most popular platform for Perl and CGI development. Of course, Perl and CGI support numerous other systems, including Microsoft’s popular 32-bit Windows systems: Windows 95, Windows 98, Windows NT, and Windows 2000 (hereafter collectively referred to as Win32 ). Throughout this book, we will focus on Unix, but we’ll also point out those things you need to be aware of when developing for non-Unix-compatible systems.

We use the Apache web server throughout our examples. There are several reasons: it is the most popular web server used today, it is available for the most platforms, it is free, it is open source, and it supports modules (such as mod_perl and mod_ fastcgi) that improve both the power and the performance of Perl for web development.

What You Are Expected to Know Before Reading

You should already be comfortable with Perl. Although the first edition of CGI Programming on the World Wide Web discussed other programming languages, this edition of CGI Programming with Perl (as the new name implies) focuses exclusively on Perl. CGI supports a wide variety of programming languages, but Perl has certainly become the language of choice.

If you do not already know Perl, an excellent introduction is Learning Perl, Second Edition, by Randal Schwartz and Tom Christiansen (published by O’Reilly & Associates, Inc.). Once you understand the basics, we strongly recommend you get a copy of Programming Perl, Third Edition (affectionately known as The Camel Book) by Larry Wall, Tom Christiansen, and Jon Orwant (O’Reilly & Associates, Inc.). It is the standard reference used by Perl developers everywhere. Additional Perl resources are listed in Appendix A.

We will discuss many modules from CPAN, the Comprehensive Perl Archive Network. If you have not downloaded and installed modules from CPAN before, refer to Appendix B.

You should also be familiar with perldoc, the standard tool for browsing Perl documentation. perldoc is useful for two reasons. First, it allows you to access the convenient and extensive documentation distributed with Perl. Second, it is essential for learning to use modules downloaded from CPAN. perldoc is also presented in Appendix B.

Overview of the Book

Chapter 1 presents a general introduction to CGI, including history, web server configuration, and a sample CGI script.

Chapter 2 through Chapter 4 cover the basics of using CGI. We begin with an overview of HTTP and then see how CGI builds upon it. We then look at HTML forms, which are a common way to pass information to CGI scripts.

Chapter 5 and Chapter 6 look at several popular modules available to help us write CGI scripts easily. We also compare different strategies for generating dynamic HTML output.

Chapter 7 looks at how a different technology, JavaScript, can be used with CGI scripts to create more powerful solutions.

Chapter 8 through Chapter 13 present solutions to challenges and tasks that CGI scripts commonly face. These chapters include general topics such as online security, storing permanent data, and tracking users across pages, as well as more specific topics such as sending email, allowing users to search your site, and creating dynamic images.

Chapter 14 covers middleware and XML, which enable your CGI scripts to provide an interface with other information servers.

Chapter 15 through Chapter 17 explain how to write better CGI scripts by discussing strategies for debugging CGI scripts, guidelines for writing good code, and how to improve performance.

The book also includes two appendixes, which contain a list of sources where you can find more information about CGI, and information on downloading code from CPAN.

Conventions in This Book

Constant Width

is used for HTTP headers, status codes, MIME content types, directives in configuration files, arrays, operators, variable names (except in examples), and computer output in text.

Italics

is used for filenames, pathnames, newsgroup names, Internet addresses (URLs), email addresses, terms being introduced, commands, options/switches, program names, subroutine names, functions, methods, and hostnames.

ALL CAPS

is used for environment variables, HTML attributes, and HTML tags (within angle brackets <>).

How to Contact Us

We have tested and verified all the information in this book to the best of our abilities, but you may find that features have changed or that we have let errors slip through the production of the book. Please let us know of any errors that you find, as well as suggestions for future editions, by writing to:

O’Reilly & Associates, Inc.
101 Morris St.
Sebastopol, CA 95472
(800) 998-9938 (in the U.S. or Canada)
(707) 829-0515 (international/local)
(707) 829-0104 (fax)

You can also send messages electronically. To be put on our mailing list or to request a catalog, send email to:

To ask technical questions or to comment on the book, send email to:

We have a web site for the book, where we’ll list examples, errata, and any plans for future editions. You can access this page at:

http://www.oreilly.com/catalog/cgi2/

For more information about this book and others, see the O’Reilly web site:

http://www.oreilly.com

Acknowledgments

Now that I have had the experience of working on a book, I will never read a list of acknowledgments the same way again. A book takes a tremendous amount of work from lots of different people, and friends and family contribute far more than I ever realized.

I would like to thank my friends and family, who were not only very understanding when this book took a great deal of my time and energy away from them, but who also never stopped asking how it was progressing and patiently listened to me whenever I bemoaned my lack of free time. Thanks also to the very friendly folks at Printers Inc. in Mountain View, where much of this book was written between refills of coffee and tea.

Thanks to Brad Ashmore at Hewlett Packard for allowing me the flexibility to work part-time while juggling work, the book, and my sanity. Thanks to Baskar Srinivasan, Natasha Fattedad, and Anh Hoang for picking up the slack. Thanks to everyone I worked with at HP for understanding when I found I could no longer keep all the balls in the air.

I’d like to thank everyone at O’Reilly. A big thanks to Linda Mui, who has been shepherding this book to completion. She was always available to answer questions and provided just the right mix of encouragement and careful critique. Thanks to Rob Romano for the illustrations and to Christien Shangraw for coordinating.

Shishir Gundavaram deserves thanks for both new material and the original edition that so many of us read and used.

A big thanks goes to the reviewers and those who provided feedback. Gunther Birznieks not only contributed chapters, but also provided a very thorough review. Nat Torkington provided an exceptionally detailed review. Others who contributed feedback include Linda Mui, Andy Oram, Dan Beimborn, Sam Tregar, Paula Ferguson, and Jon Orwant.

Finally, credit goes to the open source developers who have worked long hours to create the applications and modules discussed in this text. Without their work, the Web would not be what it is today.

—Scott Guelich July 2000

There are many people involved in the creation of a book. In particular, having the pleasure of working with both Scott Guelich and Shishir Gundavaram, two talented authors I had only met virtually before, was a really great experience. In addition, I would like to thank both Andy Oram and Linda Mui. I learned much from both of you through the course of this book.

Thanks to Lincoln Stein for originally suggesting to me that I contact Andy about helping out on the book. And also thanks to all the others at O’Reilly who were involved in bringing this book into reality. A book is truly a team effort with many people pitching in.

I would also like to thank the entire open source community for making a rewrite of the book necessary! When I look back at how many improvements have been made in Perl, web technologies, all the modules people have written, as well as the underlying global infrastructure improvements to the Web, I am astounded by what has been accomplished.

Finally, I would like to thank organizations such as the Electronic Frontier Foundation (http://www.eff.org/) for helping keep the Web and Internet as free as possible in this increasing age of legislation. Ideals such as these have founded a cyberspace where ideas and information can flow freely from every corner of the Earth.

—Gunther Birznieks July 2000

Acknowledgments from the First Edition

I’d like to thank Dyung Le for not only suggesting the idea for the book, but giving me an opportunity to develop software straight out of high school. In addition, I’d like to thank Rita Horsey, who also taught me quite a bit, and provided me with an Internet connection in the early days of the book.

Of course, I’d also like to thank my family for not only putting up with my bizarre work hours during the entire writing period, but also coming to my assistance whenever I needed it. There’s no way I could have finished this book without their support.

Thanks to all the reviewers and everyone who provided suggestions: Jeffrey Friedl (the king of regular expressions), Andreas Koenig (the father of MakeMaker), Marc Hedlund (the originator of the CGI FAQ), Tom Christiansen (the Unix wizard), Jon Backstrom, Joseph Radin, Paul DuBois, and from ORA, Norman Walsh, Paula Ferguson, Ellie Cutler, Tanya Herlick, Frank Willison, Andy Oram, Linda Mui, and Tim O’Reilly.

And last, but not least, a thanks to all my friends here and to my family and relatives in India, especially my grandparents.

Hope you find the book useful!

Shishir GundavaramMarch 1996

Get CGI Programming with Perl, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.