Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Viewing Client/Server Environment Variables - Perl

Chapter: Internet & World Wide Web HOW TO PROGRAM - Perl and CGI (Common Gateway Interface)

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Viewing Client/Server Environment Variables - Perl

Knowing information about a client’s execution environment allows system administrators to provide client-specific information.

Viewing Client/Server Environment Variables

Knowing information about a client’s execution environment allows system administrators to provide client-specific information. Environment variables contain information about the execution environment in which 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 the use of 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 the “Web Server Installation” document at www.deitel.com 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. Other important files (such as .html files,


.shtml files, images, etc.) are normally placed in the root directory of the Web server. For additional information, see your Web server’s documentation.


In Fig. 27.11, 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 statement (line 5) instructs Perl pro-grams 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 func-tions that return strings representing XHTML (or HTML) tags and HTTP headers. With the use statement, 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.

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 por-tion of the header indicates that the browser must display the returned information as an XHTML document. Standard output is redirected when a CGI script is executed, so the function print outputs to the user’s Web browser.


On 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). Notice 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 statement on 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 to this file, specified in lines 7–9. 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 Vari-ables...". Note that 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 syntaxes is to consult Official Guide to Programming with CGI.pm 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 the Web resources at the end of this chapter.)


    # Fig. 27.11: fig27_11.pl

    # Program to display CGI environment variables.

    use CGI qw( :standard );


    $dtd =

    "-//W3C//DTD XHTML 1.0 Transitional//EN\"


    print(    header() );


    print(    start_html( { dtd => $dtd,

     title => "Environment Variables..." } ) );

    print( "<table style = \"border: 0; padding: 2;

    font-weight: bold\">" );

print( Tr( th( "Variable Name" ),

    th( "Value" ) ) ); 


print( Tr( td( hr() ), td( hr() ) ) );


foreach $variable ( sort( keys( %ENV ) ) ) {   


print( Tr( td( { style => "background-color: #11bbff" }, 

    $variable ),  

    td( { style => "font-size: 12pt" },   

    $ENV{ $variable } ) ) );

    print( Tr( td( hr() ), td( hr() ) ) );


    print( "</table>" );

    print( end_html() );

Fig. 27.11  Displaying CGI environment variables


On lines 19–20, we have two more CGI.pm functions—Tr and th. These functions place their arguments between table row and table header tags, respectively. The print statement displays


<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. [Note: This function has a capital “T” because Perl already contains an operator tr.] We call function Tr again on 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 table in Perl that contains the names and values of all the environment variables. On lines 24–32, we see a foreach structure that 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 that element’s value. For this reason, hashes are also known as associative arrays, 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, that 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 loop iterates sequentially through the array returned by sort, repeatedly assigning the current key’s value to scalar $variable. Lines 26–31 are executed 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 ($vari-able) in one column and the value for that variable ($ENV{ $variable }) in the next. We call function td on line 26 again, using curly-brace notation. This line specifies the value for the attribute style. The name of the attribute is specified on the left, followed by its value on the right. When using the CGI module functions, this notation is used to specify attribute values. On line 28, we use the hash notation again to specify a style attribute. Finally, on line 35, we call the function end_html, which returns the closing tags for the page (</body> and </html>).

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.