List Values and Arrays
Now that we’ve talked about context, we can talk about list literals and how they behave in context. You’ve already seen some list literals. List literals are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it). Because it (almost) never hurts to use extra parentheses, the syntax diagram of a list value is usually indicated like this:
(LIST)
Earlier we said that LIST in a syntax
description indicates something that supplies list context to its
arguments. However, a bare list literal itself is the one partial
exception to that rule, in that it supplies list context to its
arguments only when the list as a whole is in list context. The value
of a list literal in list context is just the values of the arguments
in the order specified. As a fancy sort of term in an expression, a
list literal merely pushes a series of temporary values onto Perl’s
stack, to be collected off the stack later by whatever operator wants
the list. In scalar context, however, the list literal doesn’t really
behave like a LIST, in that it doesn’t supply list context to its values. Instead, it merely evaluates each of its arguments in scalar context, and returns the value of the final element. That’s because it’s really just the C comma operator in disguise, which is a binary operator that always throws away the value on the left and returns the value on the right. In terms of what we discussed earlier, the left side of the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access