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.