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 (Continued)
Line 11 instructs the Perl script to print a valid HTTP header, using function header from the CGI library. Browsers use HTTP headers to determine how to handle incoming data. The header function returns the string "Content-type: text/html\n\n", indicating to the client that what follows is XHTML. The text/html portion of the header indicates that the browser must display the returned information as an XHTML document. Standard output is redirected when a CGI script executes, so the function print outputs to the user's Web browser.
In lines 13-14, we begin to write XHTML to the client by using the start_html function. This function prints the document type definition for this document, as well as several opening XHTML tags (<html>, <head>, <title>, etc., up to the opening <body> tag). Note that certain information is specified within curly braces ({}). In many CGI module functions, additional information (e.g., attributes) can be specified within curly braces. The print function in lines 13-14 displays the result returned by start_html. Each argument within the curly braces is in the form of a key-value pair. A key (or value name) is assigned a value using the arrow operator(=>), where the key is to the left of the arrow and the value is to the right. The first argument consists of the key dtd and the value $dtd. When we include the dtd argument in the function start_html, the default document type definition is changed from HTML's DTD to the value of $dtd. This adds the proper XHTML DTD (specified in lines 7-9) to this file. The title argument specifies the value that goes between the opening and closing <title> tags. In this example, the title of the Web page is set to "Environment Variables...". The order of these key-value pairs is not important.
The function start_html, as well as many other Perl functions, can be used in a variety of ways. All of the arguments to start_html are optional, and some arguments can be specified differently than how we see in this program. A good way to find correct syntax is to consult the book Official Guide to Programming with The Standard for Building Web Scripts by Lincoln Stein (the creator of the CGI library). Information about CGI is also available on the Internet. (See Section 25.5, Wide Web Resources, at the end of this chapter.)
In lines 19-20, we have two more functions-Tr and th. These functions place their arguments between table row tags and table header tags, respectively. The print statement outputs
       <tr><th>Variable Name </th><th>Value</th></tr>
Function th is called twice, with the arguments "Variable Name" and "Value", causing both of these values to be surrounded by start and end table header tags. Function Tr places these two header tags inside <tr> start and end tags. [Note: This function has a capital "T" because Perl already contains an operator tr.] We call function Tr again in line 22 with the hr and td functions, in order to print a row of horizontal rules within <td> tags.
The %ENV hash is a built-in data structure in Perl that contains the names and values of all the environment variables. The foreach statement in lines 24-32 uses the %ENV hash. The hash data type is designated by the % character and represents an unordered set of scalar-value pairs. Unlike an array, which accesses elements through integer indices (e.g., $array[ 2 ]), each element in a hash is accessed using a unique string key that is associated with the element's value. For this reason, hashes are also known as associativearrays, because the keys and values are associated in pairs. Hash values are accessed using the syntax $hashName{ keyName }. In this example, each key in hash %ENV is the name of an environment variable name (e.g., HTTP_HOST). When this value is used as the key in the %ENV hash, the variable's value is returned.
Function keys returns an unordered array containing all the keys in the %ENV hash (line 24), as hash elements have no defined order. We call function sort to order the array of keys alphabetically. Finally, the foreach iterates sequentially through the array returned by sort, repeatedly assigning the current key's value to scalar $variable. Lines 26-31 execute for each element in the array of key values. In lines 26-29, we output a new row for the table, containing the name of the environment variable ($variable) in one column and the value for the variable ($ENV{ $variable }) in the next. We call function td in line 26 and use curly-brace notation to specify the value for the attribute style. In line 28, we use the hash notation again to specify a style attribute. Line 35 calls the function end_html, which returns the closing tags for the page (</body> and </html>).
Page 1 | 2 | 3