1.2 Using Scalar Variables
A scalar variable is a placeholder or a name whose value has a single component, unlike a more complex variable such as an array variable or a hash variable which can contain many parts or components. We will see arrays and hashes later in the chapter. Perl allows two types of scalar variables commonly used - numbers and strings. A number is either an integer (such as 10) or a float (such as 2.35, -1.414e-10 or 1.414E10). As mentioned earlier, a string can be enclosed either within single quotes or double quotes.
The next program shows us how to use scalar variables in Perl. We use numbers as well as strings.
Program 1.2
#!/usr/bin/perl #file scalars.pl $day = "Wednesday"; $date = 28; $month = 'May'; $year = "2003"; $space = " "; $you = "justin"; $me = 'jugal'; $high = 65.2; $low = 40.3; print "Dear \u$you,\n\n"; print $space x 5; print "How are you doing? "; print "Today is $day, $month $date, $year.\n"; print "Today's high and low temperature were $high and $low degrees F. "; $tomorrow = $date + 1; print "Tomorrow is $month $tomorrow, $year.\n"; print "\n\u$me\n";
First, we assign values to a number of scalar variables. It is not necessary to declare the variables and their types before we use them. Declaring a variable means telling the system that we intend to use a variable before we actually use it. A variable is usually of a certain type, such as scalar, array and such. We can assign a value to a variable wherever in the program we find it convenient. All scalar variables in Perl must have names that start with the dollar sign: $.
The program assigns values to a number of string variables. The scalar variables $day, $year, $space and $you have been assigned double-quoted string values. The scalar variables $month and $me have been assigned single-quoted string values. Double quoted strings must be used if we want the string to contain backslashed escape characters such as \n. In this example, the string assignments do not have any such backslashed two-character sequences although the string arguments to some of the print commands have them.
We have also used numeric variables. In Perl, although we can use integers and floats, internally all numbers are stored as double-precision floating point values. Here, $date, $high and $low are scalar variables that have been assigned numeric values. Later in the program, we see a scalar variable $tomorrow that has been assigned the value obtained by adding 1 to $date.
In this simple program, we see some interesting operations that Perl allows on strings. The first print statement.
print "Dear\u$you,\n\n";
The string argument is a double-quoted string. As a result, it can contain backslashed escape characters such as \u and \n. \u requires Perl to uppercase the next letter it prints. In this case, the next word to print is obtained by variable interpolation or by substituting a variable by its value. Since we have the \u escape character in front of $you, the first letter in the value of $you is be uppercased
before printing. That is Perl prints Justin instead of justin.
The second print statement is
print $space x 5;
The x is the string repetition operation. Here the value of the variable $space is repeated 5 times before printing. Since the value of $space is a single blank space, Perl prints five blank spaces.
The rest of the program is straight-forward.
We can create the text of the program using any text editor. Let the file where the program is stored be called scalars.pl. The output of this program is given below.
Dear Justin, How are you doing? Today is Wednesday, May 28, 2003. Today's high and low temperature were 65.2 and 40.3 degrees F. Tomorrow is May 29, 2003. Jugal
