Mike's Web

Lesson 2

  1. Arrays and Associative Arrays
  2. Processing Environment Variables
  3. A Simple Query Form


Arrays and Associative Arrays

In addition to storing data in single-value ("scalar") variables, Perl can also store data in arrays, which are lists of values, and associative arrays, which are paired or keyed arrays. For example, instead of doing:

To store a bunch of string names of colors, you could instead do:

Note that an array variable is prefixed with an @ sign; @colors means the entire array. You can also refer to the individual elements of the array by number. Perl, like many programming languages, counts from 0 when counting array elements, so in the above example,

Note: in perl the '==' operator means 'is equal to', or that the value on the left is equal to the value on the right. It should not be confused with '=', which is an assigment operator.

Another difference to note here. We used @colors to mean the entire array, and $colors[index] to refer to a single element of an array.

A second kind of array is the associative list. This is similar to a two-dimensional array, or an array of pairs. Associative lists are "keyed" arrays, meaning that instead of referring to the data by a given index, you refer to it with a key value. Here is another example of a list of colors, this time stored in an associative array:

This array is actually keyed off the color name, and the data in the array is the hexadecimal RGB value for that color. The entire array is referred to as %colors (note the difference in naming an associative array vs. a regular array), and the individual values are found as follows:

An associative array is often easier to work with than a regular one, since you don't need to know the exact index of the data value you are looking for; you only need to know the name of the key.


Processing Environment Variables

Every time a CGI is called, a set of environment variables are passed to it inside an associative array called %ENV. It's very simple to print all these values out, and some of the values in the ENV array will be useful to you later, so let's try it. Copy your first.cgi file that you wrote in lesson 1 to a new file, and name it env.cgi:

A few comments: this is your first introduction to a Perl foreach loop. The structure of a foreach loop is very simple:

The script will loop through each element of the given array, setting $loop_index to the value of the current element, until it reaches the end of the array. keys(%alist) is a Perl function that returns all of the keys for a given associative list. The result of the keys() function is a normal array.

Save the above cgi, chmod it, and call it up in your web browser. Remember, if you get a server error, you'll want to go back and try running the script at the command line in the unix shell, to see just where there might be a problem.

Here is my sample script as it runs on io.com. You may notice a number of interesting values in the list. Here are some environment variables that are most likely to be set in any CGI:

There are various others that will appear, depending on the server type and the nature of your script.


A Simple Query Form

You can set the QUERY_STRING value in a number of ways. For example, here are a number of links to the same above env.cgi script, only in each one of them, the QUERY_STRING is set to something different:

Notice how in each of the above cases, for each link you clicked on, the query string variable was set to a different value? You can hardcode the value to be passed into the URL itself.

This can be carried one step further, by setting up a simple form, using the GET method:

Notice in this example, there is no "submit" button; with the GET method you can just hit return and the form will send. Also in this example, you'll notice that the query string has two parts. It should look something like:

The value on the left is the actual name of the variable, as you set it up in the form itself. The value on the right is whatever you type into the input box, BUT you may notice if you had any spaces in the string you typed, they've been replaced with +, and similarly various punctuation and other special non-alphanumeric characters are escaped out with a %char. Since the GET method is passing along all of your data tacked onto the end of the URL itself (inside the query string), it has to convert any special or unusual characters so that the URL gets resolved properly. Your Perl script can convert this information back, but it's often easier to use the POST method when sending long or complex data. GET is mainly useful for short, one-field queries, especially for things like database searches.

Lesson 3 will cover multi-field forms, using both GET and POST methods.


Copyright © 1997 by Jacqueline D. Hamilton All Rights Reserved.


Mike's Home | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 | Lesson 5 | Lesson 6 | Generic Lesson