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
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 youre finished, youll have a solid understanding of the CGI environment and how its used to pass information between the Web page and your Perl program. Youll 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, lets 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 phrasethe stringfor 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 programs 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
Its 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 that123. 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 doesnt 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, wouldnt 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.
|