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

I simply say "a slice in scalar context returns the last item".

If you (and others) confined yourselves to "simply saying": It would be more accurate to say that 'a slice' rather than 'a list' in a scalar context returns the last item., then this exchange would probably never have taken place.

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);.

Which inevitably leads to further long, rambling tirades about how there are many different meaning of the term 'list' in Perl.

Using the term unqualified inevitably brings the language pedants crawling out of the woodwork to demonstrate their superior understanding, despite that most readers will:

  1. understand what the writer meant;
  2. intellectually gloss over any exceptional, technical inaccuracies that the unqualified use of the term might be subject to;
  3. understand that most of us neither want nor need to add a dozen levels of footnotes to our writings;
  4. nor want to bother trying to decipher the writings of those that do feel that need.

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^10: Scalar context of slice ("list")
by ikegami (Patriarch) on Oct 06, 2008 at 09:26 UTC

    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)?

      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.