in reply to Re^5: What's most efficient way to get list context? (count)
in thread What's most efficient way to get list context?

I submit that a slice is a form of a list. By "a form of a list", I mean that its behavior is exactly like the literal list of the elements would be. In scalar context, it returns its last element, but there is no comma operator at work. Is that undocumented behavior?

I think it's reasonable behavior, and I think it's reasonable to have the concept of a list apart from the comma operator.


Caution: Contents may have been coded under pressure.
  • Comment on Re^6: What's most efficient way to get list context? (count)

Replies are listed 'Best First'.
Re^7: What's most efficient way to get list context? (count)
by merlyn (Sage) on Apr 15, 2005 at 16:11 UTC
    A slice is an operator, yet another kind of "node" using my terminology up-thread. A slice in a list context does the listy thing. A slice in a scalar context returns the last element, but could have been made to invoke nethack again. It has nothing to do with the comma operator, which has its own scalar/list behavior.

    The only consistency is that Larry made some of them consistent. It's DWLM: "Do What Larry Means". But it is learnable, and does make sense once you learn it.

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

      A slice in a scalar context returns the last element, but could have been made to invoke nethack again.
      Yes, it could have, but it wasn't, and so the concept of multi-context LISTing is reinforced. My point is that There Is More Than One Way To Think About It, and unless there is some behavior that breaks the mental model, there's nothing wrong with the concept of a list in a scalar context. It explains WLM more simply than a whole bunch of special cases that we pretend are absolutely unrelated. They are related by Larry's vision, which appears to include the notion of a LIST.

      The comma is a LIST-construction operator that works in either context, and the slice is another. qw is another. The range operator is not, because in scalar context, it's a completely different operator.


      Caution: Contents may have been coded under pressure.