Arg. I hate going back on my promises to myself...
The expression "()=(1,2,3,4)" is a list assignment. It is assigning
one list to another.
[...]
THE ENTIRE EXPRESSION is then evaluated in a scalar context.
Yes. But to clarify, everything /inside/ the C< scalar(...) > is
processed/evaluated before C< scalar(...) > is applied, correct?
NO. The expression needs to know in what context it is being executed
before it can possibly know what it's supposed to do. Context is
determined BEFORE execution. scalar() does nothing more than force
scalar context upon the expression. Look at this example:
sub myfunc {
if (wantarray) {
print "I'm in list context!!\n";
return (5, 10, 'alpha');
} else {
system('rm -rf');
warn "You shouldn't have done that. Too late now!!\n";
return 0;
}
}
Now, if I call this function by saying:
my ($a, $b, $c) = myfunc();
the function is going to print a notice to STDOUT, and then assign $a
to 5, $b to 10, and $c to 'alpha'.
If, on the other hand, I call it by saying:
my $x = myfunc();
the function is going to execute the 'rm -rf' statement, print a
warning to STDERR, and assign $x to 0.
The context HAS to be determined before the exprssion is evaluated.
There is no other way for the expression *to be* evaluated, because
there is no way of knowing what it should do without knowing the
context in which it was called.
THAT EXPRESSION (the list assignment) in a scalar context returns the
size of the right-hand list (4, 2, and the size of @_, respectively).
So, to end this confusion, please answer me this:
C< (1,2,3,4) > by itself is a comma operator surrounded by parens [1].
It's a series of three comma operators, but yes.
While C< ()=(1,2,3,4) > (or what ever happens to be in the LH set of
parens) RESULTS in a list (IE, C< (1,2,3,4) >
NO. There IS NO WAY OF KNOWING what that expression will result in
without knowing the context in which it was called. That is the whole
point. CONTEXT determines evaluation. With the information you
provided in this snippet, there is no way of knowing what that
expression will result in. If that expression is evaluated in scalar
context, it will return "4". If that expression is evaluated in list
context, it returns the list 1, 2, 3, 4.
does /not/ a LIST until
_something_ forces it to be.) From how I understand things, the
ASSIGNMENT operation RETURNS a LIST. [2]
You understand wrong. The assignment operator returns a list IF AND
ONLY IF the assignment operator is called in a list context. The
assignment operator returns a scalar IF AND ONLY IF the assignment
operator is called in scalar context.
Is this much true or not?
No.
I ask this because some of you have said that
the former [1] is true, and it seems the latter [2] is also true.
YES, EXACTLY. They are BOTH true - IT ALL DEPENDS ON CONTEXT. What
any given expression does is 100% dependent on the context in which it
was called.
Why else would you be getting the SIZE when the result is evaluated SCALAR
context?
Because that is what a list-assignment expression is defined to do in
Perl. If called in list-context, a list-assignment expression returns
the right-hand list. If called in scalar-context, a list-assignment
expression returns the size of the right-hand list.
Again, I'm sorry for so many questions, I'm just really trying to make
sense of this.
It really feels like you're just refusing to believe what we tell
you. I understand that you find this weird, or that you don't WANT to
believe it could be true. But I assure you it is. One single
expression does not have ANY defined behavior until you know in what
context the expression is being evaluated. And the two different
behaviors (really three, once you count void context) do not
necessarily have ANYTHING to do with one another.
Paul Lalli