If you call a subroutine in a scalar context, that context gets propogated to the expression that is calculating the return value automatically. Likewise, a list context. This is documented, and is not an "optimization", but a definition!
Remember, it's not just about list vs length of list... see my On Scalar Context for all the ways that this introduced context can affect the result.
Thus, a subroutine called in a scalar context could never have constructed any sort of list at any time during the return. It has to construct a scalar of the appropriate flavor. And for @foo, that's gonna be the length of the foo array, and none other.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
In reply to •Re: Optimizing with wantarray
by merlyn
in thread Optimizing with wantarray
by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |