The University of Texas at Austin- What Starts Here Changes the World
Services Navigation


Echo ENV

This script gathers the CGI environment variables and prints them to the screen. In Perl, the CGI environment variables are stored in a special hash called %ENV. The script uses a loop to print each element of %ENV. Most scripts, which use environment variables, only use one or two at a time.

Open a text editor to write the script:

pico echoenv.cgi

Type the following Perl code in your text editor exactly as it appears here:

#!/usr/local/bin/perl
print "Content-type:text/html\n\n";
print '<HTML><HEAD><TITLE>echoenv.cgi results</TITLE></HEAD>';
print '<BODY BGCOLOR="FF0066">';
foreach $i (keys %ENV) {
       print "<B>$i</B> = $ENV{$i}<BR>";
}
print '</BODY></HTML>';

Save the file and exit the editor.

Make the script executable, type:

chmod 755 $HOME/public_html/cgi-bin/echoenv.cgi

This table includes a line-by-line explanation of echoenv.cgi:

#!/usr/local/bin/perl Tells the server the Perl interpreter is located in the directory /usr/local/bin/perl
print "Content-type:text/html\n\n";
Is a print statement for the HTTP header. In this case, the header tells the Web browser to display HTML formatted text.
print '<HTML><HEAD><TITLE>echoenv.cgi results</TITLE></HEAD>';
print '<BODY BGCOLOR="FF0066">';
These print statements start the HTML document, create the <HEAD> section, and begin the <BODY> section of the HTML page.
foreach $i (keys %ENV) {
       print "<B>$i</B> = $ENV{$i}<BR>";
}
This foreach loop prints the key/value pairs of the %ENV hash.
print '</BODY></HTML>'; This print statement closes the HTML document.

 

 

 


  Updated 2006 August 14
  Comments to TeamWeb