In previous part of "Start with Perl" we saw how we can
write a small Perl cgi script and run it on a web server.
In this issue we want to speak about variables in Perl
There are three types of variables in Perl language:
scalars, arrays and associative arrays. First let's speak
about scalar type.
Numbers and strings can be stored in a scalar type
variable. Scalar type variables start with a '$' sign.
Perl automatically converts between numbers and strings as
required. Look at this example (comments will come after
each statement) :
$n=' are you';
# Above statement concatenates two strings
# This one also concatenates two strings but,
# first converts them to string
# In above line string is first converted to number
Output result is:
How are you
You can run above example in command line but not in a web
server because we have not added html headers etc.
You can also use "Winperl" by Maxwell Nairin Andrews. It
has a simple IDE and runs Perl programs inside its IDE. It
is a very useful tool for learning Perl.
Now let's have a look at arrays.
Arrays are a collection of scalars. Array variable names
start with '@' sign.
Look at below example to see how we can assign values to
Array members can be used directly. You can either modify or
read the value of each member easily. But this time you must
use sign for scalar type before the name.
$langs, $langs, $langs
You can even mix scalar values in an array:
print "First one is $myarray while next is $myarray\n";
print "Third one is $myarray";
First one is 21 while next is hello
Third one is 33
And now let's see associative arrays:
Associative arrays are a list of values indexed by
subscripts. Variable name for this type must start with '%'
It's enough for this issue. We will see more complex examples
in next issue. In this issue we did not write CGI programs
but we need this information before we can write CGI programs.
Until next issue, goodbye!
If you have any question about Perl and cgi programming you
can ask it in our programming forums.