Previous Page Next Page

Chapter 16. CGI and Perl: The Hyper Dynamic Duo

16.1. Static and Dynamic Web Pages

Once you start browsing the Internet, you jump from one site to another, viewing all kinds of Web pages from simple home pages to highly developed sites, such as Google or Amazon. Even a simple Web page is a file that normally contains HTML tags and text, formatting instructions, and underlined phrases called links that connect you to other documents either on the same machine or on some other machine on the network. The document (called a hypertext document) tells the browser how to display the document; e.g., what fonts, colors, styles will be used. The page may also contain hypermedia, which includes images, sound, movies, and hotlinks to other documents. A Web page is created in a text editor, and the resulting HTML file is called the source file, which can be viewed in the browser by clicking on "View" in the menu bar.

Figure 16.1. To view the source file.


Figure 16.2. Viewing partial source page for google.com.


So that the browser recognizes the file, its name ends in either .html or .htm. The HTML tags tell the browser how to display the document on your screen. Learning the basics of HTML is not difficult, but developing an artistic and interesting design is another story, and there are thousands of companies devoted to creating these master-pieces for other companies doing competitive business over the Web.

There are two types of pages: static pages and dynamic pages. Static pages do not require interaction with the user. The most they can do is send already existing documents to users. They are analagous to a page in a book and usually describe the services some individual or company offers. They can be very artistic and interesting, but they can't handle information on demand. Dynamic pages, on the other hand, are "alive." They can accept and retrieve information from the user, produce specialized and customized content, search through text, respond with e-mail, query databases, and generate documents on the fly. They can manage information that is continually changing, based on the requests of different users. These dynamic pages require more than an HTML text file. They are driven by programs, or scripts, that interact with the Web server, which then transfers information to your browser. To send the information back and forth between the program and the server, a server-side program is used. The server itself relays user requests to a program, which in turn manages the information, such as parsing the data from a form, retrieving data from a file or database as a result of a user request, and then sending it back to the server.

The CGI (Common Gateway Interface) protocol defines how a server can communicate with programs. Its function is to allow the Web server to go beyond its normal boundaries for retrieving and accessing information from external databases and files. It is, then, a specification that defines how data can be transferred from the script to the server and from the server to the script. Gateway programs, called CGI scripts, can be written in any programming language, but Perl has become the de facto standard language, mainly because it is flexible and easy to use.

If you have read the previous chapters, you know that the Perl interpreter is easy to obtain. You know that it is portable. And you know about Perl's capability to handle regular expressions, files, sockets, and I/O. Once you know Perl, writing CGI scripts is relatively easy. The critical part is making sure that the server and Perl have been properly installed and that your scripts are placed in the directory where the server will look for them. The pathnames must also be set correctly so that the server knows where you are storing the scripts and how to reach the necessary libraries. All efforts are for nothing if any of the necessary steps from installation to implementation are incorrect in any way. It is very frustrating to see the browser whining about not finding a requested file or a server scolding that you are forbidden to run a program or that your document contains no data when you know it does.

This chapter is not written to make you a master Web designer; it is to give you some understanding of how Perl fits into the CGI scheme and how dynamic pages for the Web are created. Sometimes, seeing the overall picture is the key to understanding the purpose and plan of the more detailed design. There is a plethora of Web information available on the Internet and in trade books to fill in the details.

Today, PHP and ASP.net are server programming modules embedded in the server itself, eliminating the need for CGI scripts, but due to the popularity of Perl and the fact that there are so many CGI scripts in use, mod_perl (dubbed "Perl on steroids") allows you to embed the Perl interpreter right in the Apache server to increase Perl's performance and to accommodate the developers who prefer to use Perl to create dynamic pages and still be able to use CGI scripts with enhanced speed and power. To learn more about mod_perl, see Appendix D and http://perl.apache.org/.

Figure 16.3. The relationship between the browser, server, and CGI program.


Figure 16.4. A simple CGI program sends output to the browser.


Previous Page Next Page