Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

© 2004
pages: 1420

Amazon logo

This tutorial presents an introduction to the popular Perl programming language and to the Common Gateway Interface (CGI). Together these can be used to develop powerful Web applications.
[Note: This tutorial is an excerpt (Sections 25.1 and 25.4) of Chapter 25, Perl, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.845-846 and 857-860. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
25. 4 Viewing Client/Server Environment Variables
Knowing information about a client's execution environment allows system administrators to provide client-specific information. Environmentvariables contain information about the execution environment in which a script is being run, such as the type of Web browser used, the HTTP host and the HTTP connection. A Web server might use this information to generate client-specific Web pages.
Until now, we have written simple Perl applications that output to the local user's screen. Through CGI, we can communicate with the Web server and its clients, allowing us to use the Internet as a method of input and output for our Perl applications. In order to run Perl scripts as CGI applications, a Web server must be installed and configured correctly for your system. See Chapter 21, Web Servers (IIS and Apache), and for information on installing and setting up a Web server.
We place our CGI programs in the cgi-bin folder. If this directory does not exist, create it in the Web server's root directory. Recall that, by default, the Web server's root directory is C:\Inetpub\wwwroot under IIS and C:\Program Files\Apache Group\Apache2 under Apache. The Web server must be configured correctly to allow files in the cgi-bin directory to run as scripts. The Web server also must be running for CGI programs to execute. Please consult your Web server's documentation for additional configuration details. Copy the remaining .pl files and the password.txt file from the Chapter 25 examples directory to the cgi-bin directory. Other important files (.html files, .shtml files, etc.) normally are placed in the Web server's root directory. All image (.gif) files should be placed in an images folder within the Web server's root directory for the examples in this chapter to display properly.
CGI Script that Displays Environment Variables
In Fig. 25.7, we present our first CGI program. When creating dynamic Web pages in Perl, we output XHTML by using print statements. The XHTML generated in this program displays the client's environment variables. The use directive (line 5) instructs Perl programs to include the contents (e.g., functions) of predefined packages called modules. The CGI module, for example, contains useful functions for CGI scripting in Perl, including functions that return strings representing XHTML (or HTML) tags and HTTP headers. With the use directive, we can specify which functions we would like to import from a particular module. In line 5, we use the import tag :standard to import a predefined set of standard functions. We use several of these functions in the following examples.
Fig. 25.7 Displaying CGI environment variables.
1   #!C:\Perl\bin\perl
2   # Fig. 25.11:
3   # Program to display CGI environment variables.
5   use CGI qw( :standard );
7   $dtd =
8   "-//W3C//DTD XHTML 1.0 Transitional//EN\"
9    \"";
11  print( header() );
13  print( start_html( { dtd => $dtd,
14            title => "Environment Variables..." } ) );
16  print( "<table style = \"border: 0; padding: 2;
17          font-weight: bold\">" );
19  print( Tr( th( "Variable Name" ),
20             th( "Value" ) ) );
22  print( Tr( td( hr() ), td( hr() ) ) );
24  foreach $variable ( sort( keys( %ENV ) ) ) {
26      print( Tr( td( { style => "background-color: #11bbff" },
27                       $variable ),
28                 td( { style => "font-size: 12pt" },
29                       $ENV{ $variable } ) ) );
31      print( Tr( td( hr() ), td( hr() ) ) );
32  }
34  print( "</table>" );
35  print( end_html() );

Page 1 | 2 | 3