Paul Lalli wrote:
I understand what you are saying,
No, you really really don't.
but when it comes down to it, and from
your own words, a list is being returned from the expression,
"()=(1,2,3,4)", after which it is evaluated in scalar context.
Is this correct?
No. Not correct. Incorrect.
One last time, then I'm done with this thread. It's been explained to
you multiple times, and you're simply arguing that we're wrong at this
point.
The expression "()=(1,2,3,4)" is a list assignment. It is assigning
one list to another. Just like the expression "($a, $b) = (4, 5)" is
a list assignment. Just like "my ($b) = @_" is a list assignment.
The fact that the = operator is being used with a list on the left
side results in the expression on the right side ("(1,2,3,4)", "(4,
5)", or "@_", respectively) being evaluated in a list context.
THE ENTIRE EXPRESSION is then evaluated in a scalar context. THAT
EXPRESSION (the list assignment) in a scalar context returns the size
of the right-hand list (4, 2, and the size of @_, respectively).
After being evaluated in scalar context, you are left with
the size.
No. NO LIST is ever evaluated in a list context.
Ok, but I'm a little confused now after reading Tad's post which
seems to say otherwise:
It seems to me that lists do naturally return their size when
evaluated in scalar context, as evidenced by using " ()= "
on a raw list.
NOTHING either Tad nor I ever said says that. Rather we very
specifically are telling you that your scenario does not exist. THERE
IS NO SUCH THING as a "list being evaluated in scalar context".
Once again, the "() =" does not force the right-hand list to return
its size. It is not the list that's returning its size. It is the
list assignment expression. The entire thing. "() =" imposes list
context on the right-hand list, but this is meaningless because
nothing is being done with the values of that assignment. It is the
entire list assignment expression that, when evaluated in scalar
context, happens to return the size of the right-hand list. The
expression "()=(10,11,12)" returns 3 in scalar context. The
expression "($a, $b, $c)=()" returns 0 in scalar context. The
expression "($a, $b, $c)=foo()" returns whatever the size of the list
foo() returns is in scalar context.
So why
do functions like sort not follow this (assuming I'm not horribly
mistaken) ?
Regrettably, you are horribly mistaken.
Maybe some clarification could be in order here, please?
Because there is no reason to make sort return anything in particular
in scalar context. Why would there be? What on earth would be the
point of sorting a list but not doing anything with the sorted
results? It makes sense for grep to return the size of its resultant
list, if I simply want to know how many items the grep condition holds
for. It makes sense for map to return the size of its resultant list,
if I simply want to know how many items resulted from the
transformation (which is not necessarily the same number as the
original list). There is NO CAUSE to call sort in scalar context, and
therefore the results are undefined.
#How many of @items are greater than two?
my $x = grep { $_ > 2 } @items;
#How many items are produced by repeating each element
#two or three times, depending on even vs odd?
my $x = map { ($_) x ($_ % 2 ? 2 : 3) } @stuff
#What question is answered by this:
my $x = sort @items;
#that would not be answered by:
my $x = @items;
#?
Paul Lalli