Quoth Florian Kaufmann <>:
>
> I am still after the answer to the old question whats the difference
> between an array and a list. Yes, I read the faq, and I am reading
> Programming Perl.
An array is a variable, a list is a value. That is, the contents of an
array can be modified (by assigning to a subscript, or with push, pop,
splice &c.) whereas the contents of a list cannot. All you can do is
build a *new* list based on the old one, say with map or grep. A list
also has no 'life' beyond the end of the expression it is part of: it
must be assigned into an array or it will be garbage-collected.
(Exception: the list passed to 'return' exists past the sub return and
into the expression that made the call.)
> In my current understanding, the array subscript operator [] forces
> its left operand to be an array.
No, this is incorrect. There are three different operators in Perl
spelled []: array subscript, array slice, and list slice.
$ary[0] array subscript
$aref->[0] also array subscript
@ary[0] array slice
(LIST)[0] list slice
For the 'list slice' form, the parens are part of the syntax, so @ary[0]
and (@ary)[0] are quite different expressions.
> Thus, if that left operand is a list,
> that list is converted to an array. Thus things like
> $x = (1,2,3)[0];
> $x = (3)[0];
>
> work.
No. Otherwise exists( (1,2)[0] ) would work, which it doesn't.
> But then again, why shoudn't that work?
> $x = 3[0];
This is none of the forms above, so it is a syntax error.
> I thought it's the operator , which composes/constructs a list, not
> parantheses.
The comma only composes a list in list context. The inside of the parens
in the ()[] list slice operator are in list context, so you will get a
list to slice.
> Thus a literal scalar like 3 in 'array context' (imposed by the array
> subscript operator []) is just a list with one element, just as (3)
> [0]. Thats why I expect 3[0] to work.
There is no 'array context', at least, not in Perl 5. 3[0] is a syntax
error, so there are no contexts at all

; in (3)[0] the ()[] operator
does indeed provide list context to the 3.
I hope this helps you understand it: it *is* rather confusing

.
Ben