![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Dissecting the Example There are several things to cover before you try to run this program, so be patient! The first thing you should note is how the %EnvVarList initialization ended. The last element has no comma after it because it doesnt need one. The parenthesis to end the array and the required semicolon are down on the next line. You probably noticed one other thing higher up in the program: a require statement with a new file name. html.pl is just a consolidation of your previous two files to take care of the HTML header and ender code. Save the following code as html.pl: # html.pl # Contains header and ender subroutines for setting # up HTML documents from Perl. # Set up a standard HTML header section with the page title passed # on the command line. sub HTML_Header { print "Content-type: text/html", "\n\n"; # Put up standard HTTP ⇒ opening line. print "<HTML>", "\n\n"; # Specify HTML document. print "<HEAD>", "\n\n"; # Specify header section. print "<TITLE>", "@_", "</TITLE>", "\n\n"; # Put up the title line. print "</HEAD>", "\n\n"; # End header section. } # End HTML_Header.pl. # Set up a standard HTML footer section. At this point, it simply # ends the BODY and HTML sections. sub HTML_Footer { print "\n", "</BODY>", "\n\n"; print "</HTML>", "\n\n"; } # End HTML_Footer Remember that laziness and impatience are the hallmarks of a good programmer. Putting both of these subroutines in one file eliminates a whole line of typing! A more practical consequence is that it limits anything that can go wrong with this part of the code to one file. As a result, it will make your debugging job easier.
The each Function and while BlockThe each function is one of several Perl built-in functions that are designed to deal with the members of associative arrays.
Lets look at the code line that utilizes each: while (($EnvironVar, $Desc) = each (%EnvVarList)) First, its set up at the top of a while block, which executes in a loop until the statement in parentheses is no longer true. The assignment ($EnvironVar, $Desc) = each (%EnvVarList) places the first key-value pair in %EnvVarList in $EnvironVar and $Desc, then increments, or adds 1 to, the index of the array, bringing up the next key-value pair. When the while block reaches the end of the array, each assigns a NULL value to $EnvironVar and $Desc, which makes whiles test case false. The loop ends.
Now, lets take a closer look at the entire while block: while (($EnvironVar, $Desc) = each (%EnvVarList)) { print $EnvironVar, ": ", $Desc, $ENV{$EnvironVar}, "<BR>", "\n"; } The code the while block executes is enclosed in open and closed brackets, as usual for code blocks. In this example, while sets up a loop that will continuously print the text of $EnvironVar, $Desc, the value in the environment (remember %ENV?) associated with $EnvironVar, an HTML <BR>, (line break) and a new line. The while block loops until no more pairs can be extracted from %EnvVarList. In other words, the statement (($EnvironVar, $Desc) = each (%EnvVarList)) is false because youve reached the end of the list.
|
![]() |
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. |