in reply to Re^4: What's most efficient way to get list context? (count)
in thread What's most efficient way to get list context?
when perl is coaxed into evaluating the RHS in a list context, and then the returned list is put in a scalar context,No, you can't "put {a list} in a scalar context".
What you've done is demonstrate that the list-assignment operator ((...) = LIST) returns the number of elements assigned in a scalar context. This is both documented, and could just as easily invoke Nethack instead (although that would be less useful).
That has nothing to do with "list in scalar context", because "list in scalar context" can't exist.
It helps me to always think of the abstract syntax tree. Every node in the tree is some kind of operator/function. Every node is being evaluated in some sort of larger context (scalar/list/void, and to a lesser extent scalar can also be numeric or string). Every node can choose to return different things to each of those contexts.
In your example, the "top node" is "list assignment operator". If you evaluate that in a list context, it returns a copy of the contents. If you evaluate that in a scalar context, you get the number of elements copied.
At no time is there a "node" that has a strictly list value that is being evaluated in a larger context. In some sense, lists don't exist, except because someone applied list context to some node that can return a scalar or a list. Because there's no existing node that returns a list in a scalar context, it simply does not exist. You could probably write one in XS, and break things, but there's no existing one that does that. {grin}
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: What's most efficient way to get list context? (count)
by Roy Johnson (Monsignor) on Apr 15, 2005 at 15:18 UTC | |
by merlyn (Sage) on Apr 15, 2005 at 16:11 UTC | |
by Roy Johnson (Monsignor) on Apr 15, 2005 at 16:45 UTC |