Lesson 3 | What is CGI capable of? |
Objective | Explore the capabilities of CGI. |
Common Gateway Interface
CGI is the glue between a Web server and server-side interactive programming. The input to a CGI program, if any, originates with forms, imagemaps, or URLs, and as such can be from just about any source. The output from a CGI program can be any of the content that a Web browser can display, from dense text to custom-generated graphics. Some of the more common uses of CGI are:
- Surveys
- Email forms
- Guestbooks
- Point of sale
- Interactive maps
- Discussion groups
- Server-push animation
- Web site management
Limitations of CGI
A CGI program cannot update a Web page in place on a browser because the HTTP connection does not have a method for replacing part of an object without sending an entire new object.
Even if HTTP could update a Web page in place on a browser, HTML does not support on-the-fly page updates. This is a limitation of the protocols and languages involved.
Aside from that limitation, a CGI program can generate virtually any object that can be displayed on a Web browser.
In fact, it can even generate things that cannot be displayed on a browser. The possibilities are limited only by your imagination.
Here are some examples of CGI is being used in working projects.
Sample CGI Work
The following examples demonstrate how CGI has been used in a couple of commercial projects.
A personal growth counselor uses a "creative word play" game to help her clients tap into and express their inner thoughts and feelings. This game was adapted to web presentation using Perl/CGI, allowing clients from around the world to work with the counselor via the web.
Words are selected randomly from a hidden list, and the client's resulting word play can be emailed directly from the program to the counselor. The program presented here is virtually the same as used on the counselor's site, but it allows you to send your word play to yourself or a friend.
Web-database Integration
Another common use of CGI is in the deployment of database information via the Web. The site featured here offers visitors the opportunity to search through an extensive database of independent recording artists, albumns, labels, and musical styles. Visitors can search on any one of these aspects of the independent music scene. Results are displayed in various formats, depending on the nature of the search and which database records were found.
Perl Modules
Modules are the loadable libraries of the Perl world. A Perl module is generally just another Perl source file that defines a number of functions and/or variables, although it can also be an interface to an external C library. Modules are the main way for supporting additional functionality in your Perl scripts and for properly dividing up your module into a reusable format.
For example, we can import the CGI module, which supports a range of web-related functions and tools using
use CGI;
What actually happens is that during the compilation stage, when Perl sees require or use, it looks for a file called CGI.pm, first in the current directory, and then in the library directories for the current Perl interpreter (as defined in @INC). As soon as it finds the module, it imports the module source and then parses that as part of the main script. We do not need to worry about naming conflicts, because the package system explained earlier in the chapter will be able to determine the difference between different objects.
Perl 6
Perl CGI - Exercise
Click the Exercise link below to examine a Web site that uses a CGI program.
Perl CGI - Exercise
In the next lesson, you will find an introduction to Web servers that includes necessary information about a server's architecture,
so that you can write CGI programs that interact with the server.