Path // www.yourhtmlsource.comCGI Scripting → PERL VARIABLES

Perl Variables


Now that we've got the fundamentals of a CGI script down, we'll need to become familiar with some programming constructs, especially variables, which are the building blocks of all coding languages. In this tutorial we'll look at the various variables Perl offers.

Clock This page was last updated on 2012-08-21



Basic Variables

If you've done any programming before, even in JavaScript, you'll be familiar with variables. They can be thought of as containers for data. You give a variable a name and then give it a value, be that a number, or piece of text or whatever. You can then use the variable's value later on by calling its name, or perform operations on the variable and then use it in your script.

Perl's most basic type of variable is called a scalar variable, which can hold a single value. We prefix variable names with a dollar sign ($) in Perl. Here are some examples of declaring some random variables:

$name = "Mickey";
$secretnumber = 24;
$onequarter = 0.25;
$htmlsource = "excellent";

As you can see, number values can be given straight, and text values (called Strings in programming circles) are enclosed in double quotes. Number variables can be either single numbers or fractions (anything with a decimal place). Perl variables are not typed, which means the sort of variable you're creating (whether it is a number, boolean or text) is never defined, and so you can use a variable as whichever is necessary. We can use our variables to define other variables, even dropping them into double-quoted strings:

$foo = 6;
$lamp = "I like the number $foo";

Now if you print $lamp, it will output I like the number 6. Get it? The variable value is substituted in for its name whenever you call for it. This means you can declare variables at the start of some code, or get them from the reader, and then use them at the end; in a thank-you page for example.

Variable names which start with a letter or underscore can contain numbers after this point. Variable names which begin with a number must contain numbers exclusively.

sourcetip: Note that Perl code and filenames are case-sensitive. So, for example $perl and $PERL are two different variables. This may become another common error in your scripts.

Arrays

The second type of variable is an array (sometimes referred to as a list). As the name suggests, one of these can hold an array, or range, of values. Array names are started with an at-sign (@). The number of values an array can hold is not fixed. Here's an example:

@simpsons = ("Homer", "Bart", "Moe", "Diamond Joe Quimby");

This is an array of strings (as denoted by the quotation marks). You can have an array of numbers too. Each entry in the array is given a numerical index, so that you can call on the value in a certain position in the array. This numbering system starts with 0, so to get to the first value, you call on the single variable $simpsons[0]. Note here that when referencing individual entries, you use a dollar sign before the array name, as you're only taking a scalar (single) value.

If you wanted to write out the contents of an array, you could print each value, laboriously like so:

print "$simpsons[0]\n";
print "$simpsons[1]\n";
print "$simpsons[2]\n";

There is an easier way to do this provided by Perl, called the foreach construct:

foreach $i (@simpsons)
{
    print "$i\n";
}

What this does is set up a block of instructions between the curly braces. Then, 'for each' index in the array, perform the instructions in this block, in this case printing out its value. In some programming languages, the curly braces are unnecessary if there is only one statement to execute between them, but in Perl this is not possible. The braces are always required.

As a shortcut, an array's contents can be printed out as one value with a simple print @array; command, though this is a bit more restrictive than using a loop.

Array Operations

We need to be able to modify arrays for when new information becomes available, or take away redundant data. There are a number of functions available for adding and taking away values.

$deletedvalue = pop(@simpsons);
shift(@simpsons);

Here we have two operations — the first, pop grabs the last element of the array and removes it. In this case we've salvaged the value and assigned it to a separate scalar variable, but this is only if you still want to use the data elsewhere. Otherwise you can just perform the command, as in the second line with shift. This operation removes the first element in the array.

You can create what's known as an array slice by specifying the range of index you want to extract from an existing array. For instance, the code below will initialise a new array containing the values with indexes from 2 to 6 (inclusively) in the @example array.

@newarray = @example[2..6];

To add elements on to the end of the array with push:

push(@simpsons,"Gil");

You can even append one array to another in a similar fashion with a command like push(@simpsons,@futurama).

There are just a few more commands for arrays now. You can sort the array alphabetically with sort(@array). However, this does not actually change the original array, so you need to assign the updated array to a new variable, like so:

@sortedarray = sort(@array);

The inverse of this command is reverse, which updates the array to the inverse of what you get with sort. Again, you need to assign a new variable to keep this ordered array.

There are two methods to count the number of variables in an array (remember, it will probably change through the course of a script running). Using our original array with 4 values, $#simpsons will return the value 3, as it checks the index of the last value in the array, and since the first value's index is 0, the number you get will be one less than the number of items in the array. If you want the real number of items, use the scalar(@simpsons) operation.

Finally, if you want to concatenate an array into one string of text, use the join command and specify how you want the items separated. The following code will leave you with a string of simpsons names, with commas in between:

$simpsonsnames = join(", ",@simpsons);

Hashes

The oddly-named hash is a special type of an array called an associative array. Each entry is a pair of values. They're denoted with a percentage mark (%) and consist of pairs of keys and values. Examples:

%nationalities = ( "ross",               "irish",
                             "brad",              "american",
                             "freduardo",      "mexican"   );

As with arrays, you can target an individual element with the dollar sign and the key:

$nationalities{'ross'};

You use curly braces and single quotes to get elements here though. The value associated with the key you require is called by this code. To print out all of the values in the hash, use the foreach method again:

foreach $key (keys %nationalities)
{
    print "$key is $nationalities{$key}\n";
}

However, this will print out the keys in random order — to get them printed out as you have entered them, you have to set the order again in the foreach loop:

foreach $key ("ross", "brad", "freduardo")
{
    print "$key is $nationalities{$key}\n";
}

As you can guess, hashes come into their own when we're dealing with form data we've received from a reader (all placed into one hash), and taking individual parts out corresponding to each form element on the preceding page.

Hash Operations

delete $hash{$key}
Deletes the key and associated value from the hash, and optionally allows you to assign this value to a new variable.
exists $hash{$key}
Returns a value of 'true' if the specified key exists in the hash.
keys %hash
Returns a list of keys in the hash.
values
Returns a list of values for the hash.
scalar %hash
Checks if there are entries in the hash. Returns true if there are, false if the hash is empty.

Environment Variables

Whenever a page sends information to a CGI program, it also sends along a hash full of information about the reader who is using the page. All of this data is stored in a hash called %ENV. You can access this hash and use the values in your scripts. The variables that come in %ENV are:

DOCUMENT_ROOT
The root directory of the server (so you can use the same script on multiple sites).
HTTP_COOKIE
If you've set a cookie, this holds its value.
HTTP_HOST
The hostname of your server.
HTTP_REFERER
The full address of the page that sent this information to the script.
HTTP_USER_AGENT
The browser type of the user.
HTTPS
Returns 'true' if the server is secure.
PATH
The system path your system is running on.
QUERY_STRING
The most important variable — this is the form data brought through from the page that's calling the script.
REMOTE_ADDR
The reader's IP address.
REMOTE_HOST
The name of the user's host, if your server supports this feature. otherwise it's just the IP address again.
REMOTE_PORT
Which port of the server the user is connected to.
REMOTE_USER
If your pages are password-protected, this gives the reader's username.
REQUEST_METHOD
Whether the form was submitted using GET or POST.
REQUEST_URI
The address of the requested document or CGI file, relative to the document root.
SCRIPT_FILENAME
The full pathname of the current CGI.
SCRIPT_NAME
The interpreted pathname of the CGI itself.
SERVER_ADMIN
The email address of your server's webmaster.
SERVER_NAME
The domain name associated with your server.
SERVER_PORT
The port number your server is listening on.
SERVER_SOFTWARE
The server software and version you're using.

Some servers will send other information too, but these ones are constant. As you can see, some of these variables will be the same for every one of your users (the server-reliant ones), while the rest will be different for everybody. To use the variables, it's just as easy as any other hash, like

print "You came from <a href=\"$ENV{'HTTP_REFERER'}\">this page</a>\n";