Perl Variables: Scalars
April 26, 1999
Variables allow us to manipulate values which, as the
name implies, vary over time. Variables are used to
contain values, be they arithmetic (numbers) or strings
(a bunch of text characters), and variables can be operated
upon, meaning added, subtracted, combined, multiplied,
and so on.
The most common, basic variable in Perl is known as a scalar
variable, and is represented by a preceding dollar sign ($).
For example, imagine that we want to use a variable to keep
the current exchange rate between American and Canadian
currency, and this variable will be named "exRate".
In Perl we would refer to this variable as:
$exRate
We could simply assign a value to exRate:
$exRate=1.35;
We could create another variable, $USTotal, to represent a
dollar amount in American currency, then calculate the value
of a resulting variable, $CDNTotal, and output the results.
#!/usr/bin/perl
$exRate=1.35;
$USTotal=50.00;
$CDNTotal=$USTotal*$exRate;
print "$USTotal American dollars is equivalent ".
"to $CDNTotal Canadian dollars.\n";
This simple program assigns values to two variables, and in
the third line operates on them while assigning the results
of that operation to a third variable. The operator used here
is multiplication, represented by the asterisk (*), a typical
operator in programming languages.
Notice that the scalar variable names appear within the
double-quoted output string -- this may appear unusual to
those familiar with other programming languages, but Perl
recognizes that $USTotal and $CDNTotal are variable names
and it substitutes their values in the output. The resulting
output of the above program would look like:
|
|
Note the way the output strings have been broken up into multiple segments.
Although we could have simply typed one long line, it would have been difficult
to read within this article. To make the line more legible, the output string
has been broken into portions, with a concatenation operator,
the ".",between each
portion. The result is exactly the same as if we typed one long line.
|
50 American dollars is equivalent to 67.5 Canadian dollars.
As you can see, a scalar variable holds one "piece"
of information, known as a value. In other programming
languages, there is strict control over the "types"
of data that a value can represent. For example, a real number
is an arithmetic value containing a decimal point (e.g. 25.75).
An integer is a whole number (e.g. 25), while a string is a
collection of alphanumeric characters (e.g. "hello
1999"). Whereas other languages may demand that you declare
the type of value that a variable will contain, Perl is far
less strict. In Perl, you can assign a scalar variable with
any type of value you wish -- Perl will infer this value's
type using its intelligence. Soon, we'll see why this is so
useful.
When using scalar variables, you typically engage in one of
two types of actions: operations and comparisons.
Perl Variables: Operating on Scalars
We've already seen one example of operating on some scalar
variables -- recall line 3 of the previous example:
$CDNTotal=$USTotal*$exRate;
There are two operations here: the multiplication operator,
represented by the asterisk (*), multiplies the value of
$USTotal by the value of $exRate. The assignment operator,
represented by the equal sign (=), assigns the result of the
multiplication operation to the variable $CDNTotal.
Remember previously we opined that Perl uses its intelligence
to infer the type of value a variable contains. For instance,
when you multiply two variables, this operation only makes
sense if the two variables contain numeric values. You can't,
after all, multiply the value "chicken" by the
value "eggs". Thus, when you use an arithmetic
operator, Perl tries its hardest to use the variable's values
as numbers.
Some operators, though, are not arithmetic. For example,
consider the string concatenation operator, represented by
the dot character (.). String concatenation is the result of
squishing together two string values; for instance:
"hello" . "goodbye"
|
yields
|
"hellogoodbye"
|
Here we come to the important point: if you use an arithmetic
operator on two values which can be seen as numbers, Perl
will perform the arithmetic. If you use a string operator
on the two values, Perl will treat the values as strings. Thus:
5 + 10
|
yields
|
15
|
5 . 10
|
yields
|
"510"
|
Running Perl Programs
The Perl You Need to Know
Table 1. Basic Perl Operators
|