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


Skill 3
Connecting Perl to the World Wide Web

  Using the CGI environment
  Displaying the CGI environment
  Understanding MIME types

Once you have created a Perl program, the only way it can interact with an HTML document (also known as a Web page) is by strictly following the rules and mechanisms that were designed to make the World Wide Web work. For our purposes, the pivotal mechanism is a way to get information between the Perl program and the Web page.

The mechanism is called the Common Gateway Interface, or CGI.

In this skill, you will learn more about the rules and delve more deeply into the mechanisms that comprise the CGI. When you’re finished, you’ll have a solid understanding of the CGI environment and how it’s used to pass information between the Web page and your Perl program. You’ll also be fully acquainted with the MIME standard and how it relates to HTTP and CGI.

Using the CGI Environment

You were introduced to the CGI environment in Skill 2. Now, let’s take a look at the details of what is available to you.

Understanding Environment Variables

Recall from Skill 2 that the environment is an area of memory in which variable names are associated with string values. This has nothing to do with computer platforms; the same is true in both UNIX and Windows NT and 95. The underlying mechanisms may be different, but from the user or programmer level, the systems are nearly identical.

The example you saw in Skill 2 was for the PATH environment variable, which sets up the search path, or the directories through which the operating system will search for a program when you enter its name at the command line. From the command line, the PATH variable is set by typing “PATH=<search path>” where <search path> is the list of subdirectories that you want searched (separated by semicolons in NT or colons in UNIX).

The syntax of the command is important: VARIABLE equals STRING. The operating system performs a simple substitution. For example, if you set an environment variable FOOBAR by entering at the command line

   set FOOBAR="Fragged Uncle Bays At Romulans"

the operating system will substitute the phrase—the string—for the variable name whenever you make reference to it with %FOOBAR% in Windows 95 or NT (see Figure 3.1) or $FOOBAR in UNIX.


Figure 3.1:  Setting and printing an environment variable in Windows NT

There are no rules (until you run out of memory!) about what can be set to what in the environment. You can use any name and set it to any value, keeping in mind that you are dealing with string data.

The environment is available to any program that can make calls to the operating system; that means you can get to it through your Perl program, too. As usual, Perl makes it convenient for you by preloading the environment into a special associative array, %ENV, whenever you run the interpreter.


NOTE:  You will learn about Perl associative arrays in detail in this skill. But the environment, set up as it is in the form variable = string, is an excellent analogy. In an associative array, each member is associated with a value; for example at index 0 there actually are two members: the variable and its value. Associative arrays introduce another Perl notation, too. The names are always preceded with the percent sign (%).

If FOOBAR has been loaded into the environment as in the example above, you can put it in one of your program’s variables this way:

   $FooBar = $ENV{'FOOBAR'};

Conversely, you can set an environment variable like this:

   $ENV{'FOOBAR'} = "Fragged Uncle Bays At Romulans";
String Data Revisited

It’s a sticky point, but it is crucial to remember: strings are not numbers.

In other words, an environment variable that has been set to the string 123 is just that—123. It is not one hundred and twenty three until you run it through some kind of conversion.

It is easier to understand if you regard the string as an array of ASCII characters, 1 followed by 2 followed by 3. The ASCII character 1 doesn’t even have the value 1; it is 31 in hexadecimal, or 49 in decimal. The ASCII character with an actual value of 1 is Control-A, so you can imagine the havoc if your program were to confuse the two!

However, there are literally hundreds of ways to do the conversion and no programming language comes without several of them. Perl is especially adept.

Just be aware of the difference lest it come back to bite you. For example, if you created a list of files that were named with numbers (1, 2, 3, etc.) you would expect them to be listed in a directory in the correct numeric order, wouldn’t you? Wrong. Because the file names are treated as strings by the operating system, a file named 10 comes before a file named 2! (See Figure 3.2) Why? Because alphabetically any string starting with a 1 will be “less than” a string starting with 2, just as a string beginning with a would be listed before one starting with b.


Figure 3.2:  A directory of files sorted in ASCII order


TIP:  We just introduced the % notation for associative arrays, yet $ENV{'FOOBAR'} starts with a dollar sign. Why? Remember from Skill 1 that when you pull one member out of an array, it is a scalar value. Perl will expect it to be preceded with the scalar notation, which is the dollar sign.


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.