Each variable in Perl starts with a special character that identifies
what type of variable it is. We saw that scalar variables always start
with a $
. Similarly, all array variables start with the
character, @
, under the same naming rules that are used for
scalar variables.
Of course, we cannot do much with a variable if we cannot assign things to it, so the assignment operator works as perfectly with arrays as it did with scalars. We must be sure, though, to always make the right hand side of the assignment a list, not a scalar! Here are a few examples:
use strict; my @stuff = qw/a b c/; # @stuff a three element list my @things = (1, 2, 3, 4); # @things is a four element list my $oneThing = "all alone"; my @allOfIt = (@stuff, $oneThing, @things); # @allOfIt has 8 elements!
Note the cute thing we can do with the ()
when assigning
@allOfIt
. When using ()
, Perl allows us to insert other
variables in the list. These variables can be either scalar or array
variables! So, you can quickly build up a new list by "concatenating"
other lists and scalar variables together. Then, that new list can be
assigned to a new array, or used in any other way that list literals can
be used.