$color1 = "blue"; $color2 = "red"; $color3 = "green"; $color4 = "yellow";To store a bunch of string names of colors, you could instead do:
@colors = ("blue","red","green","yellow");
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,
$colors[0] == "blue" $colors[1] == "red" $colors[2] == "green" $colors[3] == "yellow"
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:
%colors = ("blue","#0000ff",
"red","#ff0000",
"green","#00ff00",
"yellow","#ffff00");
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:
$colors{'blue'} == "#0000ff"
$colors{'red'} == "#ff0000"
$colors{'green'} == "#00ff00"
$colors{'yellow'} == "#ffff00"
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.
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Print Environment</title></head>
<body>
EndOfHTML
;
foreach $key (keys(%ENV)) {
print "$key = $ENV{$key}<br>\n";
}
print "</body></html>";
A few comments: this is your first introduction to a Perl foreach
loop. The structure of a foreach loop is very simple:
foreach $loop_index (@some_array) {
do whatever you want;
}
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:
SERVER_NAME - the domain name of the machine serving your page
HTTP_HOST - the domain name of the machine serving your page
HTTP_USER_AGENT - what sort of web browser YOU are using
QUERY_STRING - this is set by the calling form (see below)
REQUEST_METHOD - usually with either be GET or POST
REMOTE_ADDR - your IP address
SCRIPT_NAME - the name of this script
HTTP_COOKIE - a Netscape-ism, this may not appear if your HTTP cookie isn't
set, but if it is, you can see it with this variable
There are various others that will appear, depending on the server type
and the nature of your script.
test1 test2 test3Notice 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:
sample_text=whatever+you+typedThe 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.