in reply to Re^7: Confused as to why the "casting context" is mis-behaving (clinging)
in thread Confused as to why the "casting context" is mis-behaving

So the sub "returns the array" (whatever that means) then values from the stack are copied into a list and then for() aliases them? That's a lot of extra steps you have invented. In reality (if anybody cares), the sub pushes onto the stack (copies of the scalar values from the array) and the for() iterates over what is on the stack (just using them in-place).

Maybe you'll then try to explain why "return (@a)" behaves differently than "return (@a, 'b')" and differently than "return ('b', @a)" when assigned to scalar? According to your explanation, everything is flattened to the list, and then the assignment to scalar should always pick up the last element from the list. But it does not.

  • Comment on Re^8: Confused as to why the "casting context" is mis-behaving (clinging)

Replies are listed 'Best First'.
Re^9: Confused as to why the "casting context" is mis-behaving (clinging)
by tye (Sage) on Oct 27, 2010 at 13:10 UTC

    Because the scalar context is applied before the sub returns. In a scalar context, 'return' only pushes one scalar onto the stack. (The section you quoted is describing what happens for for(fun()), which isn't a scalar context.)

    - tye        

      Because the scalar context is applied before the sub returns. In a scalar context, 'return' only pushes one scalar onto the stack. (The section you quoted is describing what happens for for(fun()), which isn't a scalar context.)

      I have to admit. One can imagine that opcode for "return" looks up the context and returns on the stack only what context desires. It could be done this way. But if you check "perldoc perlxs" section "Returning SVs, AVs and HVs through RETVAL", then you'll see, that the extension functions don't need to check the context. They can simply place the AV (array) or HV (hash) on the stack, and the caller will do the context adjustment for them.

      Well, one can still imagine, that the pure perl functions and the extension functions use different stack discipline, but somehow I find it unlikely.

      At the end of the day. For the perl programmer it does not make any difference how the internal perl stack is managed. The main point is, returning an array from function produces different results than returning a list. This is the message I was trying to convey.

      By the way. Have you ever tried

      use strict; my @a; sub canmod : lvalue { @a; } (canmod()) = qw(c d e); print join(',', @a), "\n"; print join(',', canmod()), "\n";
      Here, the array IS returned from the function :)

        I'm talking about what happens when return @array; gets run. return @array; is not implemented using XS macros.

        And Perl has special handling for dealing with (just) XS code that behaves badly and returns more than one item when called in a scalar context (I even mentioned this specifically previously -- probably in a prior thread about this ever-returning topic but one I re-read recently).

        but somehow I find it unlikely

        Yeah, I just make all this stuff up. For Python's sake, just go look at the source code if you think I don't know what I'm talking about. Wow, cling much?

        Here, the array IS returned from the function :)

        Um, actually, a single reference is returned from the function and the fact that the function was predeclared :lvalue means that the caller adds code to dereference that reference. But nice last stab at trying to cling just a little more. q-:

        - tye