in reply to Re^6: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands
Understanding the more common-place notion of right-associativity is not enough.Indeed. You must also know the return value of the operator (= in this case). But that's the same as trying to understand the result of
2 ** 3 ** 4you need to understand the return value of ** beside understanding right-associativity.
I do not know how this is actually implemented.Associativity is a parser/compiler issue. By the time the code is running, associativity no longer plays a role. As for the implementation of assignment, they are actually two different operators, scalar assignment (pp_sassign in pp_hot.c) and list assignment (pp_aassign in pp_hot.c).
And if you look in pp_hot.c, you see there's check on context to determine what is returned:
Now, I don't expect you to understand everything (neither do I), but I do hope you see that in VOID context, nothing is returned, in SCALAR context, a single value is returned, and in LIST context, more things are returned.if (gimme == G_VOID) SP = firstrelem - 1; else if (gimme == G_SCALAR) { dTARGET; SP = firstrelem; SETi(lastrelem - firstrelem + 1 - duplicates); } else { if (ary) SP = lastrelem; else if (hash) { if (duplicates) { /* Removes from the stack the entries which ended up a +s * duplicated keys in the hash (fix for [perl #24380]) + */ Move(firsthashrelem + duplicates, firsthashrelem, duplicates, SV**); lastrelem -= duplicates; } SP = lastrelem; } else SP = firstrelem + (lastlelem - firstlelem); lelem = firstlelem + (relem - firstrelem); while (relem <= SP) *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef; } RETURN;
One more piece of evidence that it isn't the scalar assignment that is reaching all the way behind, here we have a list assignment in scalar context where the scalar context isn't given by another assignment:
$ perl -E 'say scalar(() = ($a, $b, $c))' 3 $
|
---|