in reply to Optimizing with wantarray

I think you're misinterpreting the semantics and therefore the results.

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.

Replies are listed 'Best First'.
Re^2: Optimizing with wantarray
by dragonchild (Archbishop) on Aug 06, 2004 at 02:51 UTC
    So, this would basically be the difference between the LHS and RHS of the assignment?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      I'm not sure what you mean.

      The LHS of an assignment determines whether the assignment is a scalar assignment or a list assignment, which are entirely separate operations even though they are both spelled with "=".

      And based on the type of assignment operation, the RHS is evaluated in a list context or a scalar context.

      That's the way it works. Now if you could explain your question in terms of that, I'd be happy to answer. {grin}

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.