List values are denoted by separating individual values by commas (and
enclosing the list in parentheses where precedence requires it):
(LIST)
In a context not requiring a list value, the value of the list literal is the value of the final element, as with the
C comma operator. For example,
@foo = ('cc', '-E', $bar);
assigns the entire list value to array foo, but
$foo = ('cc', '-E', $bar);
assigns the value of variable bar to variable foo. Note that the value of
an actual array in a scalar context is the length of the array; the
following assigns the value 3 to $foo:
@foo = ('cc', '-E', $bar);
$foo = @foo; # $foo gets 3
You may have an optional comma before the closing parenthesis of a list
literal, so that you can say:
@foo = (
1,
2,
3,
);
LISTs do automatic interpolation of sublists. That is, when a
LIST is evaluated, each element of the list is evaluated in a list context, and the resulting list value is interpolated into
LIST just as if each individual element were a member of
LIST. Thus arrays lose their identity in a LIST--the list
(@foo,@bar,&SomeSub)
contains all the elements of @foo followed by all the elements
of @bar, followed by all the elements returned by the subroutine named
SomeSub when it's called in a list context. To make a list reference that
does NOT interpolate, see the perlref manpage.
The null list is represented by (). Interpolating it in a list has no
effect. Thus ((),(),()) is equivalent to (). Similarly, interpolating an
array with no elements is the same as if no array had been interpolated at
that point.
A list value may also be subscripted like a normal
array. You must put the list in parentheses to avoid ambiguity. For
example:
# Stat returns list value.
$time = (stat($file))[8];
# SYNTAX ERROR HERE.
$time = stat($file)[8]; # OOPS, FORGOT PARENTHESES
# Find a hex digit.
$hexdigit = ('a','b','c','d','e','f')[$digit-10];
# A "reverse comma operator".
return (pop(@foo),pop(@foo))[0];
You may assign to undef in a list. This is useful for throwing away some of the return values of a
function:
($dev, $ino, undef, undef, $uid, $gid) = stat($file);
Lists may be assigned to if and only if each element of the list is legal
to assign to:
($a, $b, $c) = (1, 2, 3);
($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
Array assignment in a scalar context returns the number of elements
produced by the expression on the right side of the assignment:
$x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2
$x = (($foo,$bar) = f()); # set $x to f()'s return count
This is very handy when you want to do a list assignment in a Boolean context, because most list functions return a null list when finished, which when assigned produces a 0, which is interpreted as
FALSE.
The final element may be an array or a hash:
($a, $b, @rest) = split;
local($a, $b, %rest) = @_;
You can actually put an array or hash anywhere in the list, but the first
one in the list will soak up all the values, and anything after it will get
a null value. This may be useful in a local() or
my().
A hash literal contains pairs of values to be
interpreted as a key and a value:
# same as map assignment above
%map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
While literal lists and named arrays are usually interchangeable, that's
not the case for hashes. Just because you can subscript a list value like a
normal array does not mean that you can subscript a list value as a hash.
Likewise, hashes included as parts of other lists (including parameters
lists and return lists from functions) always flatten out into key/value
pairs. That's why it's good to use references sometimes.
It is often more readable to use the => operator between key/value pairs. The => operator is mostly just a more visually distinctive synonym for a comma,
but it also arranges for its left-hand operand to be interpreted as a
string, if it's a bareword which would be a legal identifier. This makes it
nice for initializing hashes:
%map = (
red => 0x00f,
blue => 0x0f0,
green => 0xf00,
);
or for initializing hash references to be used as records:
$rec = {
witch => 'Mable the Merciless',
cat => 'Fluffy the Ferocious',
date => '10/31/1776',
};
or for using call-by-named-parameter to complicated functions:
$field = $query->radio_group(
name => 'group_name',
values => ['eenie','meenie','minie'],
default => 'meenie',
linebreak => 'true',
labels => \%labels
);
Note that just because a hash is initialized in that order doesn't mean
that it comes out in that order. See sort for examples of how to arrange for an output ordering.
Source: Perl data types Copyright: Larry Wall, et al. |