O'Reilly logo

Learning Perl on Win32 Systems by Tom Christiansen, Erik Olson, Randal L. Schwartz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Array Operators and Functions

Array functions and operators act on entire arrays. Some return a list, which can then either be used as a value for another array function, or assigned into an array variable.

Assignment

Probably the most important array operator is the array assignment operator, which gives an array variable a value. It is an equal sign, just like the scalar assignment operator. Perl determines whether the assignment is a scalar assignment or an array assignment by noticing whether the assignment is to a scalar or an array variable.[36] For example:

@fred = (1,2,3); # The fred array gets a three-element literal
@barney = @fred; # now that is copied to @barney

If you assign a scalar value to an array variable, the scalar value becomes the single element of an array:

@huh = 1; # 1 is promoted to the list (1) automatically
          # that is, @huh now is (1)

An array variable name may appear in a list-literal list. When the value of the list is computed, Perl replaces the array variable name with the current values of the array, like so:

@fred = qw(one two);
@barney = (4,5,@fred,6,7);  # @barney becomes 
                            # (4,5,"one","two",6,7)
@barney = (8,@barney);      # puts 8 in front of @barney
@barney = (@barney,"last"); # and a "last" at the end
# @barney is now (8,4,5,"one","two",6,7,"last")

Note that the inserted array elements are at the same level as the rest of the literals: a list cannot contain another list as an element.[37]

If a list literal contains only variable references (not expressions), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required