There is a lie that a list in scalar context yields the last element of the list. This is a lie because (as the saying goes) you can't have a list in scalar context. What looks like a list in scalar context is really the comma operator in scalar context and it has different behavior in scalar context.
However, there seems to be a loop hole in this logic: the null list (sometimes called the empty list). The characters ()
are defined to be the null list by perldoc perlglossary
. The construct
my $s = ();
is valid code and returns undef
to $s
. This does not appear to be documented anywhere in perldoc
(I haven't checked the Camel), but lots of code counts on it, so I think it is here to stay.
Now that the preamble is done, here is the question: if we cannot have a list in scalar context, then what do we call the empty list in scalar context and what is the rational for not calling it a list (since there are no commas to be in scalar context)?
If you are enjoying this question, you may also like the discussion going on in P5P.
It's more like a valueless expression which is equivalent to
undef
. Some more examples:List is a very generic word. You could possibly be referring to the list operator or to a list value.
There is no comma in the code, so there is no list operator.
There is no list context in the code, so there is no list value.
Therefore, there is no list in
Parentheses never create a list
(Only indirectly when on the LHS of an assignment operator.)
Perl calls it a "stub" (as shown below), and that's truly what it is. It's a placeholder in the code where putting literally nothing would be disallowed.
The stub is represented by "empty parentheses", so that's another name for it.
I call it bad code. If you want to assign
undef
, assignundef
.No, that's true. List values cannot exist in scalar context, so that leaves the list operator.
The list operator aka the comma operator returns the last element of the list in scalar context.
Compare the following. No mention of list:
There is a mention of a list
...and it has nothing to do with the parens