Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


What Is CGI, Anyway?

Does Common Gateway Interface mean much, if anything, to you? Probably not. However, CGI really is the raison d’être for this book. Without it, there would be no reason to talk about Perl and Web pages because there would be no way to link the two.

CGI as a concept has been applied to many systems other than links between Web servers and application programs. For example, it would provide a clean and near-universal interface for database servers and their clients without the barriers introduced by proprietary systems. Software manufacturers sometimes seem to worry about making sure that you only do business with them, but a “common gateway” from one system to another provides a standard of sorts to which the manufacturers have to adhere; if they can’t deal with it, no one will buy their applications.

For now, anyone who actually knows what you’re talking about when you bandy about terms such as CGI will assume that you’re talking exclusively about World Wide Web applications. In that context, without the Web, there would be no CGI. And without the Internet, there would be no Web.

CGI: The Force Behind the Web

Where HTML gives the World Wide Web its look, CGI makes it functional. It is what its name implies: A “common gateway” between the Web server and applications that can be useful to the server, but doesn’t run as a part of it. CGI is the only way the server can communicate with these other applications, such as a database.


NOTE:  Keep in mind that no support exists for CGI outside of HTTP servers. In other words, CGI only works with HTTP servers. Its uses outside that realm have been interesting, but strictly marginal.

A Common Gateway

In technical terms, a gateway is an interface or an application that allows two systems to pass information between them.

For example, Microsoft’s old Mail program and its newer Exchange are limited to sending mail only to other Microsoft Mail users. A separate product provides a Simple Mail Transfer Protocol (SMTP) Gateway so that mail can be sent to and received from the Internet.

Likewise with your Web server. It doesn’t know Perl from Adam, but through the mechanism of CGI it can handle requests from “clients,” or visitors to your site, and pass the results back.

Because the server is only following a set of rules for passing information, it does not know or care what you use in the background to process what it sends you. The functions are totally independent of one another. Thus, you can write CGI programs in any programming language. The only requirement is that the information you send back has to be formatted in a way that the server recognizes.


TIP:  You can find a great deal of information on the formal CGI specification at http://hoohoo.ncsa.uiuc.edu/cgi/interface.html.

The CGI Environment

MS-DOS, UNIX and, to a limited extent, Windows users should be at least a little familiar with the concept of environment variables. For example, on both MS-DOS and UNIX, an environment variable called PATH stores the list of directories through which the operating system will search when you type a program name on the command line.

To the operating system, whether it’s Windows or UNIX, the environment is a block of memory where variable names can be stored as string values, such as PATH=c:/bin;c:/usr/bin;c:/usr/local/bin. Taking this example further, whenever the user refers to %PATH% (on NT and 95) or $PATH (on UNIX), the operating system substitutes “c:/bin;c:/usr/bin;c:/usr/local/bin”.

Programs can get into this block of memory, too. What makes this facility especially useful is that the environment is in global memory, which means that anything there is accessible by other programs running at the same time.

The Web server fills in a standard list of environment variables when it runs; it fills in others when requests are made of it. Because the Web server runs all the time, anything it places in the environment can be read by another program, such as your Perl script, if the other program knows the names of the variables to read.

In the simplest sense, this is how CGI gets information between the server and your program (see Figure 1.11). The details are a little more complicated, and you’ll learn about them in Skill 3.


Figure 1.11:  The Web server, CGI, and the environment

CGI Programming Languages

This book deals with Perl as the preferred programming vehicle for CGI applications. However, as you have learned previously, the HTTP-CGI gateway has no requirements or preferences when it comes to the language in which a CGI application is written. Let’s examine briefly the advantages and disadvantages of some of the most widely used languages.

C, C++

C, and more recently, C++, are the most popular languages for application and systems development. Figure 1.12 shows a snippet of code in C++.


Figure 1.12:  A C++ code snippet—this is very similar to C.

Advantages

These are some of the advantages of C and C++:

  When it comes to sheer, raw power, it is very difficult to beat these two compiled languages for either CGI or normal applications. For extremely large and complicated CGI projects, C or C++ probably is a better choice than Perl, especially on a busy Web site where processing speed will be a concern.
  In addition, both languages are common on both UNIX and Windows NT systems, so there are generally few problems porting code between the platforms.
  The popularity of C/C++ means that there is a large body of existing code that you can tap into.

Disadvantages

Some of the disadvantages of these two languages are:

  As you learned earlier, there are so many nifty shortcuts built into Perl that you generally can accomplish a lot more in a lot less code than you can in C or C++. Perl’s string-manipulation functions, especially, are so much stronger that it’s almost ridiculous to try to do the same thing in C/C++.
  Here’s something else to keep in mind: It would not be an exaggeration to say that upward of 90 percent of all CGI programs involve heavy string manipulation.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.