my $mind = ( 'a', 'little', 'confused' );
And it's because perldoc perlfaq4
explains the line above as follows (emphasis added):
Since you're assigning to a scalar, the righthand side is in scalar context. The comma operator (yes, it's an operator!) in scalar context evaluates its lefthand side, throws away the result, and evaluates it's righthand side and returns the result. In effect, that list-lookalike assigns to
$scalar
it's rightmost value. Many people mess this up because they choose a list-lookalike whose last element is also the count they expect:my $scalar = ( 1, 2, 3 ); # $scalar gets 3, accidentally
What I understand this to mean is that there is no such thing as a list in scalar context.
However, ikegami maintains that it "result[s] in a list operator, so it is a list literal."
So, is it a list or not?
A list literal is something that is actually a list written out in code, so (1, 2, 3)
is a list literal, whereas caller
for example is a function that could return a list or a scalar depending on context.
In a line like:
my $x = ...;
the ...
sees scalar context, so if ...
was a list literal, then you would have a list literal in scalar context:
my $x = (1, 2, 3);
but the list literal does not result in a list, because the comma operator it contains sees scalar context, which then results in it returning the last item of the list literal, and throwing the remaining values away after evaluating them.
In terms of a function, the function itself sees whatever context it is called from, which then is propagated to any line in that function that returns. So you can have a function in scalar, list, or void context, and if the last line of that sub happens to be a list literal, that list literal will see any of those contexts and will behave appropriately.
So basically this is a terminology distinction, with list literal
referring to a comma separated list of values in the actual source code*, and list
referring to a sequence of values placed onto perl's stack.
You can write subroutines with return values that either behave like arrays or like list literals with regard to context.
sub returns_like_array {my @x = 1..5; return @x}
sub returns_like_list {my @x = 1..5; return @x[0 .. $#x]}
*or something that results in a list of comma separated values, like a qw()
or a fat comma =>
or a hash or array slice.
You can also look at my answer here: How do I get the first item from a function that returns an array in Perl? which goes into a bit more detail about lists.
I agree with you and with perlfaq4
. To quote perldata
, which is probably the definitive documentation on this point:
In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element, as with the C comma operator.
(emphasis mine).
That said, ikegami is entitled to use whatever terminology (s)he wants. Different people think of different language constructs in different terms, and as long as the end results are the same, I don't think it matters if their terminology differs from that in the documentation. (It's not a great idea to insist on idiosyncratic terminology in a public forum, though!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With