Back in January 2014 said:
Quoth (e-mail address removed):
Yes, an assignment whose LHS is explicitly parenthesised is always a
list assignment.
A little more on lists and assignments.
There was a discussion at work that touched on the difference between
"array" and "list". There was an assertion along the lines of "lists
*do not exist* in scalar context". In composing a reply, including
why I don't agree with that mental model, I tried this (lots of
extraneous stuff there from previous versions):
$ perl -e 'sub foo {13} sub bar { 14 } sub baz { @a = (5,6,7); @a }; (1 ? $x : @y) = (foo(), bar(), baz()); print $x, "\n"'
I was hoping that it would show that the list on the right could be
evaluated either in a scalar or a list context. But the parens needed
around the ?: operator made it a list LHS, so the RHS was evaluated in
a list context, so it prints
13
just as if I'd written "($x) = (13, 14, 5, 6, 6);".
Only after I rechecked "man perlop" did I realize that ?: has higher
precedence than =, so the parens weren't necessary.
$ perl -e 'sub baz { @a = (5,6,7); @a }; 0 ? $x : @y = (13, 14, baz()); print "x=$x", ", y=", join(" ", @y), "\n"'
x=, y=13 14 5 6 7
$ perl -e 'sub baz { @a = (5,6,7); @a }; 1 ? $x : @y = (13, 14, baz()); print "x=$x", ", y=", join(" ", @y), "\n"'
x=3, y=
But then I tried with a variable, in a test program
sub baz { my @a = (5,6,7); @a };
foreach (0, 1) {
$_ ? $x : @y = (13, 14, baz());
print "x=$x", ", y=", join(" ", @y), "\n";
}
exit 0;
I got the error
Assignment to both a list and a scalar at local/test/117.pl line 3, near ");"
Apparently 1?: and 0?: did compile-time constant folding and avoided
the problem.
The ": lvalue" attribute for a sub only allows a SCALAR lvalue, so
I can't bury the conditional in a sub.
So I don't know of any way to write
... something ... = (13, 14, 5, 6, 7);
in Perl so that it doesn't know at compile time whether the right-hand
side is in a scalar or list context.
But
sub foo {
return (1, 2, 5, 3);
}
doesn't know from just that code what the context is, so I think this
sub is enough of an example that it's not so useful to say "lists do
not exist in scalar context": I aver that it's better to think that
there's a list there, and in a scalar context it is evaluated to its
last element.