Instant Web Scripts with CGI/PERL Introduction
Who This Book is For
This book was written primarily for the intermediate level CGI developer who may be working for a company which is providing a Web site or who may be doing freelance work for companies on the Web. This type of developer is assumed to be fluid in HTML and will be at least comfortable in the Perl-CGI environment.
But, it is also for the beginning CGI developer who wants a simple, straight forward example-based way to learn CGI. The examples in this book have been explained simply enough that we hope they will be accessible to all and we have taken great pains to isolate the basic installation and usage issues into their own section so that the non-programmer need not spend much time deciphering the code if all they want to do is run the scripts..
Finally, this book is for advanced CGI developers who are looking for good, well documented tools that they can add to an existing arsenal of routines. The scripts in this book have been chosen because they are all in high demand in the marketplace and are the types of programs that the advanced CGI developer should be able to install on-the-fly.
What Do You Need To Know To Start
The book assumes at least a basic understanding of Perl 4.0 which has been set as the minimum standard for this book because it is still the standard for most Web servers. Perl 5.0, though an excellent upgrade, is still widely unsupported. Thus, all the applications in this book have been made to be Perl 4.0 compatible.
The book also assumes a working knowledge of the HTTP and HTML protocols which drive CGI. Though we are careful to do our best to explain these protocols when necessary, this book is not meant to be an introductory resource on protocols. There are dozens of excellent books focusing on those basic issues available, so there was no reason for us to include those discussions here. In particular, we recommend, "An Introduction to CGI/Perl" by Steven Brenner and distributed by MIS Press as a perfect complement to this work.
So why exactly did we choose to use the programming language Perl for the CGI applications in this book? Could one use another language like C, C++, Apple Script or Visual Basic instead?
These are good and extremely frequently asked questions. In fact, CGI applications can be written in any programming language which is able to accept and process input and which is able to output the results of that processing. However, for this book and for most of the CGI applications we have designed, Perl has been far and above the best choice. Specifically, we have chosen to use Perl for two reasons: 1) Perl is the right tool for the job and 2) Perl is easy.
Perl is the Right Tool for the Job
Perl does not attempt to be a super language. In fact, compared to some more robust languages out there, Perl solves only a few, though crucial, problems. Fortunately however, because Perl is specialized, it does the jobs it sets out to do exceptionally well. And, better yet, the limited set of problems that Perl can solve happens to correlate exactly well with the demands of CGI. Perl and CGI are simply a match made in heaven. Common Gateway Interface (CGI), as its name implies, provides a "gateway" between a human browser with unexpected and complex needs, and a powerful but command/logic oriented server. As a gateway, CGI must perform two tasks very well.
CGI as a Translator
First, CGI must be a good translator. All CGI applications must translate the needs of clients into server requests and translate server replies into meaningful and well-presented "answers". This can be quite a chore since computers and humans typically speak very different languages.
As such, CGI must be adept at manipulating text - be it the text inputted by the client or the text received from the server. A CGI application must be able to take "strings" of data and translate them from one language to another constantly and quickly. As we will see throughout this book, Perl has a wide variety of tools with which to manipulate strings of data. It is, in fact, one of the best languages we know of for string manipulation.
CGI Across Platform
Second, CGI must serve as gateway for as many types of clients and servers as possible. It must be more than a bilingual translator, it must be multilingual, providing translation services between dozens of browser types, server types and operating systems. Fortunately, Perl is highly portable. Due to the hard work and good intentions of many net hackers, Perl has been ported to just about every operating system you would want to run a Web server on. By pointing your web browser to:
you can download a Perl interpreter for your local operating system with no difficulties. And better yet, the Perl you write for your UNIX server today will later be portable when you add an NT server to your network tomorrow.
The fact is that for the most part, though other compiled languages may boast ten times the power and speed and offer complex functions up the yazoo, for the demands of most CGI applications which are ultimately at the mercy of bandwidth speed and limited enough in scope to not demand much gusto, a compiled language is like bringing in the hydrogen bomb to kill an ant. Perl is simply the right tool for the job. Other languages typically overkill the problem.
Perl is Easy
Perl is also easy to understand. Because Perl is an interpreted language, for example, there are no compilers and no illegible machine code compiled applications. What you see is what you get. The code that is run by the Web server, is the code that you see in your text editor window.
Since Perl is simple in design, it is also easy to modify, maintain and customize (which is really where the cost of software comes from anyway). That is, because Perl source code is so legible, it is very easy for one to pick up a script and quickly modify it to solve similar or new problems. Perl is a cut and paste language and logic is easily transferred and manipulated between projects.
The benefit of this of course, is that Perl is supported by a wide body of CGI freelance programmers. Unlike more cryptic languages like C++ or Visual Basic, Perl is accessible to anthropology majors and computer science majors alike. In fact, newsgroups like comp.lang.perl are often too prolific to frequent on a regular basis. The Perl community is thriving and thanks to the web, expanding rapidly.
Thus, since so many people can write and modify Perl, it is very easy for you to find someone to do it for you cheaply and to do it well. You need not kneel at the mercy of the few reclusive wizards of other arcane languages who confidently slide on the curves of supply and demand. There is an abundance of qualified, starving undergraduate and graduate students with skills enough to solve most of your programming needs for very cheap rates, especially if they are given a working code to modify (like the code in this book) rather than asked to write one from scratch.
How This Book is Structured
This book is divided into seven parts, each made up of several chapters and a few supplementary appendices. Both the parts and the chapters are organized according to the "process" that we use when we are installing scripts on new servers. This process includes 1) getting scripts and integrating them with your server environment, 2) understanding the logic of the scripts, and 3) customizing them according to local needs. For example, most chapters are organized into three parts: 1) an overview, 2) a discussion of application specific installation and usage, and 3) a detailed description of the algorithms used to make the scripts run.
The Overview attempts to outline the purpose of the script, the metaphors involved or the problems we sought to solve. We have attempted in this section to define the applications broadly and perhaps suggest various uses for them that we have come across while installing them for different clients.
Installation and Usage
The installation and usage section outlines the steps that you will need to take in order to port the scripts to your own server. Most often, this section will contain information on setting permissions, a discussion of defining server specific variables and options and instructions relating to the process of running the script from the Web. It is our hope that the intermediate-level developer will be able to install the distributed, generic versions of all the scripts in this book by reading only the installation and usage section.
The Design Discussion
The design discussion outlines line-by-line, the methods we used to make the scripts work. Inevitably, you will want to customize the generic scripts for local needs. If so, the design discussion is written to communicate the entire logic of the script so that you will easily be able to modify the code to your own needs. It also provides an excellent CGI-by-example tool for learning to write your own code.
Your Path Through the Parts
The organization of the seven parts of this book follow the same logic. Though we have tried to modularize each Part well enough so that it can stand alone, it is probably a good idea for you to read Part One and Two before you go about trying to install one of the CGI applications in the other Parts. Part One provides essential information about how to install scripts in general and part Two provides detailed explanations of the shared libraries used repeatedly by applications throughout this book. Once you finish reading these two Parts however, you should feel free to skip to any chapter in any of the other Parts you are interested in.
Part One: Building, Using and Installing Scripts
Part One deals with obtaining and installing scripts because that is the first thing that you will be concerned with when trying to find scripts to suit your project needs. Chapter One outlines the steps that you will need to take in order to find CGI scripts on the web, download them to your local server, and uncompress and unarchive them so that you can use them there. It also overviews the methods you will need to know in order to set the correct permissions and find the system files necessary to actually run the scripts.
Chapter Two outlines various methods that you may need to utilize in order to trouble shoot scripts in case more significant customizations are required for your server or your particular need.
Finally, Chapter Three discusses Intranet design issues providing an analysis of how Internet CGI scripts can be utilized by an Intranet.
Part Two: CGI Libraries
Part Two deals with managing routines and libraries. These chapters are extremely vital because they represent the very foundation of most scripts. Understanding libraries will be the first step towards understanding entire applications.
Chapter Four introduces the concept of using algorithms, subroutines, and libraries in preparation for the rest of the Part. This chapter includes a more theoretical discussion of web-based software design which may be very useful for the beginning developer.
Chapter Five through Ten outline the libraries that are used continually throughout this book discussing such valuable algorithms as form data reading and parsing, authentication, file locking and emailing.
Part Three: Databases
Part Three is dedicated to a discussion of web-based databases. The Part begins with the fundamental application, the Database Manager in Chapter Eleven. The Database Manager allows a database administrator to manipulate a server-based database from the web. Chapter Twelve introduces a database search engine that is meant to accompany the Database Manager for users of the database.
Chapter Thirteen discusses the Groupware Calendar, one very useful modification of the basic management and searching features incorporating its own set of date generating algorithms into the equation.
Chapter Fourteen involves a second example of a modified database management system, the Classified Ad Manager, to further exemplify the ways you might modify the search logic from the original in Chapter Twelve.
Finally, Chapter Fifteen provides a look into Intranet Database integration with a discussion of a SYBASE-based address book.
Part Four: Working With HTML
Part Four is dedicated to the discussion of working with HTML from within CGI applications. These applications are more focused on user interaction and HTML than server resources as the database applications were.
Chapter Sixteen introduces a keyword search engine that can be used by a web site administrator to provide keyword searching functionality to a web site. Using this application, visitors can search a web site, including all HTML directories.
Chapter Seventeen outlines a web-based HTML shopping cart system which is one of the more popular applications right now. Such an application affords clients the ability to browse a companies inventory via HTML pages and order items for purchase.
Chapter Eighteen provides a means for site administrators to keep track of the daily usage of their pages. By using the Publisher's Suite, a site administrator can keep track of such vital statistics as who has visited which pages and how often.
Chapter Nineteen provides a CGI backend to feedback and comment forms which can allow clients of your web pages the ability to send their valuable comments and criticisms to you while they are browsing your pages.
Finally, Chapter Twenty outlines the Web-based Guestbook which is a perennial favorite on the web.
Part Five: CGI-based Graphics and Animation
Part Five takes a quick look at CGI-based animation using the non-parsed header method to generate server push animations.
Chapter Twenty-One discusses several methods which one could use to animate text and Chapter Twenty-Two outlines the graphics based animations of random banners.
Part Six: Miscellaneous Applications
Part Six overviews several miscellaneous applications that you can use to greatly enhance the value of your web site.
Finally, the appendix includes discussions of Perl 4.0 and 5.0 which may be valuable for the reader who still has questions about basic Perl Syntax. The appendix also contains a cursory discussion of how the accompanying CD-ROM is organized.
Some Closing Words
These Parts and Chapters are designed to walk you through obtaining, installing, understanding, and modifying scripts in the ways that we have found most efficient. Unlike most of the CGI books out there, this book is written for the real world programmer who may not be interested in theory right off the bat but may need to get a complex CGI application up and running in short order. In the process of installing it, however, we hope that the theory seep in by example because we have attempted to imbue the very structure of the Parts and Chapters with our experiences installing scripts on the web.
Good luck and be a good netizen. We will all be living much of our lives on the net in the future. Support the free flow of information and be everyone's teacher and student. Information wants to be free. Support public domain software applications and their authors.