Web Perl   «Prev  Next»

Lesson 11 CGI variables
ObjectiveGenerate List of Standard Environment Variables used by CGI

List of Standard Environment Variables used by CGI

Learn how to generate a list of the standard environment variables used by a CGI program.
CGI programs get information about the server, the client, and some limited information about the user through a set of standardized environment variables.
In Perl, the contents of the environment variables are available in the form of a hashed array called %ENV. The keys of the hash are the names of the environment variables. As an example of how to access the environment variables, the following short CGI program lists the names and values of all the variables available in your environment.

#!/usr/bin/perl
$CRLF = "\x0d\x0a";
print "Content-type: text/plain$CRLF$CRLF";
foreach $varname (sort keys %ENV) {
	print "$varname: $ENV{$varname}\n"
}

Current Environment

This program simply lists all the variables defined in the current environment, which is available in Perl as the special hash %ENV.The use of hashes will be explained further in Module 2.
When you run that program on your server, you will see what variables are available in your particular environment.
There is a finite list of variables that are part of the CGI specification. Because the standard CGI variables are passed as part of the environment, other variables will also be present, but they may not be present on another system.
The program listed above will list all of the variables available on your system.
Section 2.2 of The CGI Book begining on page 26 contains a detailed description of each of the standard CGI environment variables.