go ahead... be a heretic | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
So, what value do you see in advocating this model?
I prefer to stick to models that, well, model what actually happens. And what actually happens is that the , operator really does have a scalar-context mode that gives the left operand void context and the right operand scalar context. However, in the case of a literal list returned by a sub, there are complications. Context in perl is somewhat limited. It can either be derived at compile time or looked up at runtime. If the latter, it is the context in which the current sub, eval, or mainline code is called (which itself may or may not be known at compile time). But this isn't complex enough to fully handle a literal list. When a literal list appears in list context, each component should have list context. This works fine. When a literal list appears in void context, each component should have void context. This also works fine. But when a literal list appears in (not known at compile time) scalar context, every component but the last should have void context. And perl's context propagation isn't smart enough for that. Instead every component gets scalar context and a cleanup operator (named "list") ensures that in fact the list of results is reduced to the last scalar. This is quite clearly a case of a list in scalar context. The following code demostrates this, using a tied array as a left operand to a comma operator in each of void, scalar, and list context, first where the context is known at compile time, and then where it isn't: Note that where the scalar context is known at compile time, the left operand to , got void context, but where it was not known, it got scalar context. In reply to Re: If you believe in Lists in Scalar Context, Clap your Hands
by ysth
|
|