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


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 doesn’t 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.


TIP:  In addition to calling in the necessary code, the require statement in the cgienv.pl script refers to a directory separate from the default for CGI scripts. It also specifies the path to it. This is not strictly necessary, but it’s wise for a couple of reasons. First, you are testing programs in a directory away from the one your visitors will use (so they won’t get any nasty surprises from your unfinished programs); second, you want to ensure that Perl can find the file when it executes the require statement.

The each Function and while Block

The each function is one of several Perl built-in functions that are designed to deal with the members of associative arrays.


TIP:  Two other Perl functions are used most commonly with associative arrays: keys returns a normal array of all of the keys (the first values) in the associative array; values does the same for the second values.

Let’s look at the code line that utilizes each:

  while (($EnvironVar, $Desc) = each (%EnvVarList))

First, it’s 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 while’s test case false. The loop ends.


TIP:  NULL is ubiquitous in Perl programming. You can think of it as being more zero than zero, absolutely nothing. Many Perl functions return a NULL value to indicate they have failed in some way. In our example, when each returns NULL, the array is finished. It also ends the while loop because while (NULL) is always false.

Now, let’s 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 you’ve reached the end of the list.


WARNING:  Note carefully the number of parentheses in the while statement, too. Open parentheses must match closed parentheses! If you want to see what happens when they don’t match, take one out and prepare for a scolding from the Perl interpreter. It’s one of those seemingly dumb things that you will spend much of your time debugging.


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.