in reply to Re^9: Scalar context of slice ("list")
in thread Scalar context of slice

Instead we get long, rambling, high-horse rants about how "there is no such thing as a list in a scalar context", when there patently is: print scalar(1,2,3);.

Since even that list doesn't return a list, would it be accurate to say "a list can't be returned in scalar context" (for when it happens, Perl core dumps)?

  • Comment on Re^10: Scalar context of slice ("list")

Replies are listed 'Best First'.
Re^11: Scalar context of slice ("list")
by BrowserUk (Patriarch) on Oct 20, 2008 at 02:12 UTC
    Since even that list doesn't return a list, would it be accurate to say "a list can't be returned in scalar context"

    Sorry for the delay in getting back to you, but this was one of those things that I had to allow to wash over me for a while before deciding how to respond.

    If for no other reason than you believe it is more accurate, then I would have to conclude that it probably is more accurate. And as a generality, most of what you say is accurate. That's (for me) a given.

    But the real question is: whom does that greater accuracy benefit?

    I thik we can agree, this (1,2,3) is a list; and this return (1,2,3) a return statement; and this

    sub x{ return(1,2,3); }

    is a function (attempting) to return a list.

    If we place a call to that function in a scalar context my $x = x(); then the return statement doesn't cease to be a return statement, nor does the list cease to be a list. At some point, the list is transformed to a scalar, which, in this case, happens to be the last element of the list.

    So at that point, immediately before the transformation--and the very reason the transformation is performed--the list is in a scalar context.

    Now you could conclude either of:

    • If a return statement with a list argument, is found, (or finds itself), in a scalar context, that it transforms the list to a scalar and returns that scalar.
    • Or: The return statement returns the list, and when it (the list), finds itself in a scalar context, it transforms itself to a scalar, prior to the assignment.
    • Or: The when assignment operator is given a list to assign to a scalar, it transforms the list to a scalar.

    But whichever way, and to be honest it does matter which is more accurate, the list existed; the scalar context existed and the list was within the auspices of that scalar context; and so, was transformed.

    So, I see no clearer, simpler or conceptually more accurate way of describing that than: "the results of a list in a scalar context is the last element of that list".

    And this does not apply to map & grep in scalar contexts, not because the description or concept is wrong, but because those functions do not attempt to return a list when they find themselves in a scalar context. They instead choose to return something, a scalar, that is considered more useful.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I thik we can agree, this (1,2,3) is a list;

      By many definitions, it is. It is a list literal. It becomes a list operator when compiled. A list is created on the stack when executed. However, it doesn't necessarily return a list.

      Now you could conclude either of:

      • If a return statement with a list argument, is found, (or finds itself), in a scalar context, that it transforms the list to a scalar and returns that scalar.
      • Or: The return statement returns the list, and when it (the list), finds itself in a scalar context, it transforms itself to a scalar, prior to the assignment.
      • Or: The when assignment operator is given a list to assign to a scalar, it transforms the list to a scalar.

      One could conclude one of those items, but they'd be wrong by doing so. All three conclusions are predicated on a list being passed to return, but that's not the case. A function's return statement never sees a list when the function is called in scalar context.

      In this case, return is passed the result of a list operator. When executed in scalar context, a list operator returns a scalar.

      nor does the list cease to be a list.

      Saying the list ceases to be a list is interestingly correct since the list reduces itself to a scalar.

      And this does not apply to map & grep in scalar contexts [...] because those functions do not attempt to return a list when they find themselves in a scalar context.

      Neither does a list. We've already seen the code.

      They instead choose to return something, a scalar, that is considered more useful.

      So does a list.

      So, I see no clearer, simpler or conceptually more accurate way of describing that than: "the results of a list in a scalar context is the last element of that list".

      It's no clearer than the original disputed statement ("there is no such thing as a list in scalar context") for reasons that should be obvious from this thread (different understandings of what consists a list).

      But as clear, simple and accurate as it might be, it's totally useless. The disputed statement is used to describe how operators and functions behave in scalar context. It can't be replaced with a statement that's specific to one operator.

        Okay. If accuracy is so important, then I guess I should wish you luck in balancing yourself upon your buttocks and thighs, with your torso more or less upright, atop your above-average statured, domesticated, odd-toed, solid-hoofed, herbivorous, ungulate quadruped mammal.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.