![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Writing the Display ProgramThe program that you will now writecgienv.plwill load each of the environment variables listed in Table 3.1 and a short description into an associative array called %EnvVarList. Then it will set up an HTML document and print the names and values of each of the variables until it runs through the entire array. The first element of the array is a good place to start to see how associative arrays are initialized, or loaded with data.
%EnvVarList will be fairly long, so you will set it up in a way that will make it more readable. The first few lines will look like this: %EnvVarList = ( 'AUTH_TYPE', 'Server\'s authentication method is: ', Take careful note of several things here before you go further. %EnvVarList= is familiar, as is the open parenthesis to begin the array. Notice, though, that the parenthesis is on the next line and indented. It doesnt have to be there; in fact the entire array initialization could be strung out on one line.
You will enclose the string elements of the array in single quotes this time for consistency between the keys and the data. However, that presents a problem in the first descriptionthe apostrophe in server's would normally end the string and cause the Perl compiler to complain vigorously about the rest of the array. To get around this, you simply escape the apostrophe by putting a backslash in front of it: server\'s. Finally, note that the two elements you have initialized so far are separated with a comma. Are you ready to begin the program? Enter the following code in your text editor and save it as cgienv.pl. #!c:/perl/bin/perl # cgienv.pl # # A Perl program to list some of the common CGI environment variables. # Coincidentally, we cover associative arrays and the each() function # here, too. require "perl-cgi/html.pl"; # Call in HTML header, ender. # Fill an associative array with the variable names and # short descriptions. %EnvVarList = ( 'AUTH_TYPE&', 'Server\'s authentication method is: ', 'CONTENT_LENGTH', 'Length of any attached information: ', 'CONTENT_TYPE', 'Type of any attached information: ', 'GATEWAY_INTERFACE', 'CGI version on server is: ', 'HTTP_ACCEPT', 'Client will accept these MIME types: ', 'HTTP_REFERER', 'Referrer\'s URL is: ', 'HTTP_USER_AGENT', 'Client\'s browser is: ', 'PATH_INFO', 'Extra PATH information is: ', 'PATH_TRANSLATED', 'PATH translation is: ', 'QUERY_STRING', 'Query string sent by client is: ', 'REMOTE_ADDR', 'Client\'s IP address is: ', 'REMOTE_HOST', 'Client\'s domain name is: ', 'REMOTE_IDENT', 'Identity data sent by client is: ', 'REMOTE_USER', 'User ID sent by client is: ', 'REQUEST_METHOD', 'Client\'s request method is: ', 'SCRIPT_NAME', 'URL of the CGI application is: ', 'SERVER_NAME', 'Server\'s computer name is: ', 'SERVER_PORT', 'Connection made at this port: ', 'SERVER_PROTOCOL', 'Server is using this protocol: ', 'SERVER_SOFTWARE', 'Server is running this software: ' ); # Start up an HTML document, then run the variables out to # it with their descriptions. &HTML_Header ("Common CGI environment variables"); print "<BODY>", "\n"; print "<H1>Some of the common CGI environment variables</H1>", "\n"; print "<H3><I>Note: Some variables have no value. "; print "These will be blank.</I></H3>", "\n"; print "<HR>", "\n"; # Draw a rule. while (($EnvironVar, $Desc) = each (%EnvVarList)) { print $EnvironVar, ": ", $Desc, $ENV{$EnvironVar}, "<BR>", "\n"; } # Ship the HTML ender. Were done. &HTML_Footer; # End cgienv.pl
|
![]() |
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. |