|No such thing as a small change|
Re: If you believe in Lists in Scalar Context, Clap your Handsby ig (Vicar)
|on Oct 29, 2008 at 05:53 UTC||Need Help??|
There appear to be two possibilities for LISTs:
One is that expressions that look like LISTs are LISTs regardless of whether they will be evaluated in list or scalar context, but the value and the process of evaluation depends on the context, as is generally the case with Perl.
When a LIST is evaluated in scalar context every element of the list is evaluated in scalar context and the value of the last element is the value of the LIST, with all the other values being discarded.
The other possibility is that expressions that look like LISTs are LISTs if and only if they are being/will be evaluated in list context but they are "what apperas to be a LIST"s if they are being/will be evaluated in scalar context. Commas in the part that is a LIST or a "what appears to be a LIST" are either list argument separators (not operators) or comma operators respectively. A statement may contain a LIST or a "what appears to be a LIST" depending on the context in which it is being/will be evaluated at run time and the rules for evaluating a LIST are different from those for evaluating a "what appers to be a LIST". The LIST only exists if the expression is being evaluated in list context and the "what appears to be a LIST" only exists if the expression is being evaluated in a scalar context. Therefore, there is no possibility of a LIST being evaluated in scalar context, avoiding considerable confusion.
When a "what appears to be a LIST" is evaluated in scalar context, by virtue of the comma operator, every element in the "what apperas to be a LIST" is evaluated in scalar context and the value of the last element is the value of the "what appears to be a LIST" with all the other values being discarded. (Note: all but the last element may be evaluated in void context rather than scalar context, but it still counts as scalar context for functions that care whether or not they're being called in list context.)
The former model is simpler and less surprising to me. It seems entirely consistent with the general rule that the interpretation of operations and values depends on context.
Whether any particular version of perl evaluates what appears to be a LIST in scalar context by evaluating all the elements in scalar context and using their values to initialize an ephermeral list value then discards all but the last value, or whether it has been optimized to evaluate all but the last element in void context (which is just a special case of the scalar context) and evaluate only the last element in scalar context and uses its value to initialize an ephemeral scalar value doesn't seem relevant to my understanding of Perl.
I suggest that the definition of Perl should be a simple and unsurprising specification of what the result of executing a Perl program will be, not a detailed specification of what perl will do when executing it.