=
) for
assignments. In the usual case, you'll have a single variable on the left
hand side of the equal sign, and some kind of expression on the right hand
side; the value of the expresssion will be assigned to the variable. So a
very simple example of an assignment statement would be
$x = 7;
In perl, the result of an assignment statement is the value of the expression being assigned, which allows a few constructions which can save lots of typing. For example, if you wish to assign several variables the same value, it can be done on a single line:
$i = $j = $k = 0;A very common perl idiom is to perform an assignment, and then immediately use the assigned variable in another expression. To insure that perl does just what you want, the assignment statement is surrounded by parentheses. This allows you to copy a value to a variable, and modify it in a single step. For example, the increment operator (see Section 3.3) allows you to increase a variable's value by one, by appending it with two plus signs (++). The following example shows how you could copy the value of
$x
to $y
, and simultaneously
increment $y
, without modifying $x
:
$x = 5; ($y = $x)++;After these statements are carried out,
$y
will be equal to
6, but $x
will still be equal to 5.
To assign values to a list, you can used a parenthesized list of comma-separated
elements. For example, to create a list called @values
with numeric
values of 7, 19, 23 and 14, you could use
@values = (7,19,23,14);You can use a similar statement to assign values to selected elements of an array, by indexing the array to be assigned with an array (or literal list) representing the subscripts of the elements to wish you are assigning values. Thus, to changed elements 2 and 4 (that is, the third and fifth elements - remember perl uses zero-based arrays) of the
@values
array
to 20 and 40 respectively, you could use either of the last two statements:
@which = (2,4); @values[@which] = (20,40); @values[(2,4)] = (20,40);If the list on the right-hand side contains a different number of elements than the expression on the left-hand side demands, perl will silently ignore the assignments for which no values are available, or values for which no assignment is specified.
Hashes can be assigned in exactly the same way as arrays, by alternating the
hash's keys with its corresponding values in the left-hand side list. The only
difference is that you assign the values to a hash, that is, a variable whose
name begins with a percent sign (%
).
%sidekicks = ("laurel","hardy","batman","robin", "abbott","costello");Individual elements of the hash, which are scalars, can be accessed as follows:
print $sidekicks{"laurel"}; # prints hardy print $sidekicks{"batman"}; # prints robinAn alternative approach, utilizing the
=>
operator, allows
you to specify the keys of a hash without using quotes:
%sidekicks = (laurel => "hardy",batman => "robin", abbott => "costello");If any of the keys contain blanks or special characters, they will need to be enclosed in quotes, even when the
=>
operator is used.
Another useful feature of the assignment statement in perl is the ability to assign several elements on the left hand side of an assignment when the expression on the right hand side returns an array. In its simplest form, you can use a list expression on either side of the equal sign:
($x,$y,$z) = (17,14,13)
$x
will have the value 17, $y
the value 14, and
$z
the value 13.
More commonly, a function which returns an array will appear on the right-hand
side of the equal sign, and a parenthesized list to which the elements of the
array will be assigned to appears on the left. For example, the split
function (See Section 4.3) breaks up a string into ``words'', and
returns the results in an array. To break a name into two pieces with
split
and assign the pieces to two variables at once, you could use
the following statements:
$name = "Dudley Doright"; ($first,$last) = split(' ',$name);would result in
$first
equal to ``Dudley'' and
$second
equal to ``Doright''. If any elements of the target
list are lists themselves, the first one to appear will receive any remaining
elements; the elements in the target list appearing after the first list will
all be set to undef.
$string = "one two three four five"; ($a,$b,@rest) = split(' ',$string); ($x,@rest,$y,$z) = split(' ',$string);In the first example,
$a
will be equal to ``one'',
$b
will be equal to ``two'', and @rest
will be a list
of length three,
with elements ``three'', ``four'', and ``five''.
In the second example $x
will be equal to ``one'',
@rest
will be an array of length four containing the remainder of the
the strings, while $y
and $z
will be undefined.