Home / code / cgi

Variables in Perl CGI

In Perl you have 3 different types of variables: scalar variables, arrays and associative arrays(hashes). Each variable is preceded by a different symbol: scalar variable by a $, a array by @ and associative array (hash) by a %.

Scalar variable

A scalar variable is a single value. Scalar variable can have strings or numeric values. Each scalar variable is preceded by a $. Values are assigned by by =.
Example:

$var=10
$string="Perl"
An example of use of scalar variables in a script:
$num=30;
$string="Perl";

print "Double: the string is $string and the number is $num \n";
print 'Singles: the string is $string and the number is $num \n';

Strings can be used in different ways. If they are placed between double quotes (") then they are interpreted. If they are placed between single quotes (') they are literally printed.

If you want to increase a variable with 1, you can do this as follows: $num=$num+1. But there is a shorter one: $num++. This is called auto-increment. There is also auto-decrement: $num--.

An example on how you can use "the short way":
$num=10;

print "\$num is $num \n";

$num++;

print "\$num is $num \n";

$num--;

print "\$num is $num \n";

$num+=3;

print "\$num is $num \n";

$num*=2;

print "\$num is $num \n";
In the previous example we use backslash (\) to undo the meaning of the symbol $. This makes sure we don't point to the value but the name $name.
The backslash can also be used to undo the meaning of other special characters including itself.
print "The MS-DOS path is c:\\perl\\ \n";
(\n means 'end of line', return, enter)

The print function can use the comma (,) to separate different parts. The following example explains:

$var="Perl";

$num=10;

print "Two \$nums are $num*2 and adding one to \$var makes $var++ \n";

print "Two \$nums are ", $num*2 ," and adding one to \$var makes ", $var++ ,"\n";

The first print function will interpreted the calculation as text, while the second print function will execute the calculations.

You might be surprised why the second print function doesn't Perm. This is because $var++ first shows the value and then increases. It is called post-increment. It is more clear if you add print "\$var is now $var \n" to the script:

$var="Perl";

$num=10;

print "Two \$nums are $num*2 and adding one to \$var makes $var++ \n";

print "Two \$nums are ", $num*2 ," and adding one to \$var makes ", $var++ ,"\n";

print "\$var is now $var \n";
If you wish the calculation happens before the print function you have to use ++$var. This is called pre-increment.

Arrays

A array is a ordered list of scalar variables. You can point to the complete list or to a element of the list. Every array is presided with @. The following example shows the array @names with 5 values:

@names=("Tom","Frank","Brigitte","Eli","Bert");

print "The values of \@names are @names \n";

print "The values of \@names are ", @names ,"\n";

print "The first value is $names[0] \n";

print "The last value is $names[4] \n";

print "The last value is $names[$#names] \n";

There is a difference between print "@names" and print @names. It the first case the names are separated by a space, in the second case the are colled together.

Below is a list of much used function that are used with arrays. These are the functions: push, unshift, pop, shift and scalar.

@names=("Tom","Frank","Brigitte","Eli","Bert");

print "The values of \@names are @names \n";

print "The values of \@names are ", @names ,"\n";

push (@names, "Jean");

print "After push the values of \@names are @names \n";

unshift (@names, "Danny");

print "After unshift the values of \@names are @names \n";

$last=pop(@names);

$first=shift(@names);

print "Pop removes the last value of \@names ($last) and 
	shift removes the first value of \@names ($first) \n";

print "There are ", scalar(@names) ," values in the array \n";
What have we learned here:
  • push: Adds a value to the end of the array.
  • unshift: Adds a value at the beginning of the array.
  • shift: Removes the first value, but it also returns it so it can be used in a variable.
  • pop: Removes the last value, but it also returns it so it can be used in a variable.
  • scalar: Gives the number of variables in the array.

You can change the order of the array with the reverse function or with the sort function. The reverse function reverse the list (last becomes first, first becomes last, ...), while the sort function order the list from small to big (alphabetic).

Example:
@names=("Tom","Frank","Brigitte","Eli","Bert");

@names=reverse(@names);

print "@names \n";

@names=sort(@names);

print "@names \n";

Associative Arrays (hashes)

A associative Array or hash is also a ordered list of scalar variables. But instead of using a fixed index-number, it uses a index-field (string). Each hash is preceded by a %.

@myarray %myhash
Index number Value Index number Value
0 Belgium BE Belgium
1 Netherlands NL Netherlands
2 France FR France
3 Germany DU Germany
4 Luxembourg LUX Luxembourg

In the next example you see how they are declared and used in scripts

@myarray=("Belgium","Netherlands","France","Germany","Luxembourg");

%myhash=("BE","Belgium","NL","Netherlands","FR","France","DU","Germany","LUX","Luxembourg");

print "$myarray[0] \n";

print "$myhash{'BE'}\N";
To request a value you use $. Instead of [] you use {}. In a hash you can never have 2 the same index fields.
%myhash=("BE","Belgium","NL","Netherlands","FR","France","DU","Germany","LUX","Luxembourg");

$myhash{'PT'}="Portugal";

delete $myhash{'NL'};
The above example adds and removes entries.
%myhash=("BE","Belgium","NL","Netherlands","FR","France","DU","Germany","LUX","Luxembourg");

@list1=keys(%myhash);

@list2=values(%myhash);

print "@list1 \n";

print "@list2 \n";

 

TOP