Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Setting the Environment Variables

Environment variables can be set by any program. However, programs can run programs, and the environment of a parent application also can be made available to a child application.

This concept is especially important in operating systems such as Windows 95, NT, and UNIX, which can run more than one program at a time.


NOTE:  Windows 95, NT, and UNIX are known as multitasking operating systems because they can run several applications at once. The multitasking capability allows you to run, or spawn, other applications from within your program. True to the biological analogy, the spawned program is known as a “child,” while the spawning application is the “parent.”

The reason you can set environment variables from the command line and then read them from a program is because the command line itself is a program—cmd .exe or command.com in the Windows world and the shell in UNIX—and it is the parent of every program you run. Similarly, if your program adds to the environment any other variable settings, this information is available to any child applications it spawns.


WARNING:  Environment variables in 95, NT, and UNIX last in memory as long as the program that sets them lasts. When the program exits, the variables go away.

So, if the child is written to know in advance what the names of its parent’s environment variables are, it can also read and process the values in them (see Figure 3.3). The parent can see the changes, too. It amounts to a stripped-down version of interprocess communication (IPC), which is how very sophisticated software systems coordinate themselves.


Figure 3.3:  Child programs can read and change their parent’s environment variables.


NOTE:  IPC is a topic on which some computer scientists have based entire doctoral dissertations. It is a tedious and complex subject, and far beyond the scope of this book. But rest assured, the CGI environment provides you with all of the IPC capabilities you need for Web work.

The CGI pipeline allows you to regard the Web server as the parent application and your Perl program as the child. Thus, whatever the server places in its environment comes down the pipeline to you.

CGI Environment Variables

When your Web server starts, it fills in a group of environment variables that describe some of its characteristics. Then, when a client—a visitor to your site—connects with the server, other environment variables are filled in with information about the connection: who, what, where, when, and so on. See Table 3.1 for a listing of some of the common CGI environment variables.

Table 3.1: Some Common CGI Environment Variables

Variable Description

AUTH_TYPE Authentication method for access
CONTENT_LENGTH Size in bytes of any attached information
CONTENT_TYPE Type of any attached information
GATEWAY_INTERFACE CGI version running on the server
HTTP_ACCEPT MIME types accepted by the client
HTTP_REFERER URL of the referring agent
HTTP_USER_AGENT Client’s Web browser software
PATH_INFO Extra PATH information for the CGI application
PATH_TRANSLATED PATH_INFO translated to physical file mapping
QUERY_STRING URL-encoded information sent from an HTML form by the GET command
REMOTE_ADDR IP address of the client
REMOTE_HOST Domain name of the client
REMOTE_IDENT Identity data sent by client
REMOTE_USER User identification sent by client
REQUEST_METHOD Client’s method for request (usually HTML GET, POST, or ISINDEX)
SCRIPT_NAME URL of the CGI application
SERVER_NAME The name of the server’s computer
SERVER_PORT TCP/IP port where connection was requested (usually 80 for HTTP)
SERVER_PROTOCOL Name and revision of protocol used for request
SERVER_SOFTWARE Name and revision of server software

In this skill, you will write a Perl script that grabs some of the most common variables and displays them on a Web page.

Some New Perl Concepts

In the process of designing a Perl program to print CGI environment variables to a Web page, you’ll learn the details of Perl associative arrays and two new tools:

  Using the each function to pull out the members of an associative array
  Using while code blocks to set up loops

An associative array is very much like a normal array, except that there are two values at every index position instead of one. The first value is known as the key and it is used by Perl in an internal index of the array. The internal index is constructed by hashing the keys, an efficient method of storage that makes lookups very fast.


NOTE:  Hashing is another of those really complex concepts of academic computer science that could take a book to explain. Briefly, the key-value pairs are translated—or hashed—into a numeric key that points to the value. In a Perl associative array, the hashed keys are sorted numerically in memory, so any process that looks up values based on these keys will be quite efficient. A consequence of this method, as you will see, is that the key-value pairs probably won’t be sorted in the order in which you entered them. However, Perl provides several easy ways to get around this, as you will see later in this skill.

Associative arrays usually are used for long lookup tables, where a short key can be used to refer to a long description. You will be doing just that in the program you’re about to write.

As mentioned previously, associative array variables introduce a new Perl notation—they always start with the percent sign (%).

We’ll discuss the each and while statements after you finish with the program.

Displaying the CGI Environment

It will be easier to understand the CGI environment if you can “see” it, and the interaction between the Web server and a Perl script is best illustrated with a Web page. Let’s write a Perl program that displays some common CGI environment variables in an HTML document.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.