Server-Side Includes and its Extensions
Random and Recursive Crypting using Salt on Unix and Win32
Creating a mailing list using Perl
Reading and Writing to Files on the Server
Server Side Includes and CGI Security
You are here: irt.org | Articles | CGI & Perl | Introduction to CGI [ previous next ]
Published on: Saturday 4th April 1998 By: Jason Nugent
This first article in the CGI section of "No Content" will discuss exactly what CGI is all about. Why should you use it? What can it do? What can't it do? What this first article won't do is get into any actual CGI applications. Like the Java section, you are going to have to sit through a little bit of information before playing with the toys. Patience, young Skywalker.
CGI is an acronym that stands for "Common Gateway Interface". This interface provides a means for your browser and the server where your document resides to communicate and pass information back and forth. Primarily, this is done through the <FORM> tag, but there can be other ways to use CGI effectively, like through Server Side Includes. We will see them later on. We shall also examine the different ways to transfer information from a form to a server using GET or POST. They are quite different, so we will examine how to code for each of them in turn.
CGI is great for adding interactivity to your site. There are some things that JavaScript and other client-sided techniques simply can't do, and this is where CGI comes in. Since client-sided techniques are implemented in the browser, it is difficult, if not impossible, to remember state information (other than using a JavaScript cookie). To accomplish this, you have to implement a CGI-based solution. CGI applications can interact with databases, catalogs, search engines, guestbooks, counters, you name it.
Well, there are a couple of things here that are worthwhile mentioning. As with all things that must travel back and forth across a network, CGI applications can be slowed down considerably if network is slow. On top of that, if your script is long or has to do a lot of processing, your visitor will have to wait a bit until your script is finished running.
Probably the biggest concern with CGI programs is security. Whenever you let anyone on the internet run a script located on your server, you are taking a risk. Things that you can do to minimize this risk is to code defensively, like checking for malicious user input, and also taking advantage of pre-defined environmental variables to control who has access to your script. These things will be covered in a later article.
I may be biased, but it has become quite apparent that Perl is the language of choice for developing CGI applications. There are several good reasons for this. Probably the best reason is that Perl can do wonderful things with text, which makes it ideal for web-based applications. In addition to this, Perl is relatively easy to code in, especially if you have programming experience in other languages. Be warned, though, that Perl can become very cryptic if whoever coded the script used Perl's special variables and didn't bother to comment properly. Perl has a shallow learning curve at first, but the curve can be a very long one should you choose to delve into advanced Perl development. Always remember, though, that in Perl, "there is more than one way to do it."
One thing I should point out is that Perl is NOT just for CGI programs. Perl also finds itself quite at home in the Unix environment where system administrators rejoice in Perl's ability to perform administrative tasks on a server. It has many built-in functions that duplicate regular system functions like making and removing directories, creating files, and getting system information.
Because I like Perl so much, the CGI applications that I demonstrate in these articles will be written in Perl.
There are a number of books out there that claim to make you a Perl-guru in as little time as possible. To help you weed through the lot of them, I will recommend a few books that should speed you on your way to developing CGI applications with Perl. They are:
Learning Perl, 2nd Edition - probably THE best book ever written as an introduction to Perl. The 2nd edition now covers Perl 5 and there has also been a section added on Object-oriented programming. Everything in this book is relevant since you can't really write any sort of code in Perl without using the concepts in this book. A MUST read.
Programming Perl, 2nd Edition - the Camel book. Everyone bow down and pay homage to this magnificent piece of text. Written by Larry Wall (the creator of Perl), Tom Christiansen (a Perl guru), and Randall Schwartz (the author of Learning Perl), this book contains a thorough reference to every possible thing you might ever want to do in the language. If you are serious about programming in this language, there should be a copy of this book on your desk somewhere.
CGI Programming on the World Wide Web - a book devoted to developing CGI applications for the 'net. It starts right at the basics, and works quite quickly into writing CGI apps in Perl. I recommend this one if you are serious about CGI development.
Mastering Regular Expressions - This book has changed my life. Regular expression support is arguably Perl's greatest feature and this book will teach you how to unlock it. Since a large section of CGI programming involves manipulating text, regular expressions are invaluable. Read this book as many times as you can.
If you are interested in finding out more information about Perl there are a number of places you can go.
First and foremost is to the Perl homepage at http://www.perl.com
You can also check out the Perl institute at http://www.perl.org
If those don't point you in the right direction (or if you are looking for code), head off to CPAN at http://www.perl.com/CPAN/ which is a great resource and should be used whenever you get the chance.
Server-Side Includes and its Extensions
Random and Recursive Crypting using Salt on Unix and Win32
Creating a mailing list using Perl
Reading and Writing to Files on the Server
Server Side Includes and CGI Security
CGI - Server Side Processing of Form Data