Introduction
This book is intended for web designers, entrepreneurs, students, teachers, and anyone who is interested in learning CGI programming with Perl. You do not need any programming experience to get started; if you can write HTML, you can write CGI programs. If you have a website, and want to add guestbook forms, counters, shopping carts, or other interactive elements to your site, then this book is for you.
What is CGI?
"CGI" stands for "Common Gateway Interface." CGI is one method by which a web server can obtain data from (or send data to) databases, documents, and other programs, and present that data to viewers via the web. More simply, a CGI is a program intended to be run on the web. A CGI program can be written in any programming language, but Perl is one of the most popular, and for this book, Perl is the language we'll be using.
Why learn CGI?
If you're going to create web pages, then at some point you'll want to add a counter, a form to let visitors send you mail or place an order, or something similar. CGI enables you to do that and much more. From mail-forms and counter programs, to the most complex database programs that generate entire websites on-the-fly, CGI programs deliver a broad spectrum of content on the web today.
Should you learn it?
When this book was first published, CGI and Perl was one of the few options for adding dynamic content to a website. These days it's largely been left behind as newer languages have become popular. Learning Perl/CGI probably won't get you a job, because hardly anyone uses it anymore. But if you're a tinkerer — a do-it-yourselfer who wants to learn to code and customize your website — this could be for you.
Perl is an incredibly useful language; I think of it as my secret super-power. ;) It's awesome for reading, writing, parsing, reformatting, and summarizing text files, databases, log files, raw data... you name it. It's useful not just for CGI scripts but for any kind of automated scripting or data processing. And it's available on most every Unix system, from your desktop Mac to high end cloud servers to the tiny Raspberry Pi. There's also a huge library of free, pre-written modules for Perl, which you can download from CPAN.
Learning CGI/Perl may not get you many points on your resume, but it's easy to learn, and you may find it a valuable tool to have in your developer's toolkit.
Why use this book?
This book will get you up and running in as little as a day, teaching you the basics of CGI programs, the fundamentals of Perl, and the basics of processing forms and writing simple programs. Then we'll move on to advanced topics, such as reading and writing data files, searching for data in files, writing advanced, multi-part forms like order forms and shopping carts, using randomness to spice up your pages, using server-side includes, cookies, and other useful CGI tricks. Things that you've probably thought beyond your reach, things you thought you had to pay a programmer to do . . . all of these are things you can easily write yourself, and this book will show you how.
You can also try it out before buying the book; the first six chapters are available online, free of charge, at http://www.cgi101.com/book/.
What do you need to get started?
You should already have some experience building web pages and writing HTML. You'll also need Perl and a web server (such as Apache) that is configured to allow you to run your own CGI programs.
The book is written towards CGI programming on Unix, but you can also set up Apache and Perl on Mac OS X and Windows.
- On Windows, you can download the free community edition of ActivePerl.
- Mac OS X: how to configure Apache; where to write your programs
- Unix: How to upload programs to your Unix-based ISP; Unix tutorial.
You can also sign up with a web hosting service that supports CGI, such as shared hosting on
Note: CGI.pm is no longer part of the standard Perl distribution, so you may need to jump ahead to the Perl Modules chapter and learn how to install the CGI.pm module before working through the examples. (If you're using an ISP, they may already have CGI.pm installed for you.)
Working Code
All of the code examples in this book are available on the web at http://www.cgi101.com/book/. You can download any or all of them from there, but do try writing the programs yourself first; you'll learn faster that way.
Conventions Used in this Book
Perl code will be set apart from the text by indenting and use of a fixed-width font:
print "This is a print statement.\n";
Unix shell commands are shown in a bold font: chmod 755 filename
Each program in the book is followed by a link to its source code:
Source code: http://www.cgi101.com/book/chX/program-cgi.html
In most cases, a link to a working example is also included:
Working example: http://www.cgi101.com/book/chX/demo.html
Each chapter has its own web page at http://www.cgi101.com/book/chX, where X is the chapter number. The full text of chapters 1-6 are online; other chapters include an index of the CGI programs and HTML forms from that chapter, links to online resources mentioned in that chapter, questions and answers relating to the chapter material, plus any chapter errata.
What's New In This Edition?
The 2nd edition of CGI Programming 101 has been substantially revised from the first edition. You'll learn about Perl modules from the beginning, and work with modules (including the CGI.pm module, which offers many great features for writing CGI programs) throughout the book. You'll learn how to password protect an area on your website, how to build an online catalog with a shopping cart, how to work with cookies, how to protect your site from spammers, and much more.
So turn to Chapter 1, and let's get started.