in reply to Re: Scalar context of slice
in thread Scalar context of slice

Why doesn't scalar context of a slice (as follows) give the length of the list?

Quite simply because when I noticed that using a slice in scalar context caused Perl to core dump, I patched it to return the last item instead. Actually, that was probably only for hash slices (it was a long time ago).

More sensibly, it is good that the simple typo/thinko of @array[$index] still produces the same value as $array[$index], not just being a silly way to write 1. What justification do you have to proclaim that the size of the slice is the best choice?

A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar.

map and grep return lists so, obviously, in a scalar context they also return the last item (according to you). Except they don't, of course.

I'm repeatedly surprised at who next spouts this "a list in scalar context" meme that refuses to die. It is a seductive meme, but it is at least misleading (or just "wrong", if you ask me).

Expressions that return a list of values in a list context can choose to return whatever makes the most sense when they find themselves in a scalar context. For grep the number of items makes good sense. For map, I don't think that returning the count is the best choice, but that ship sailed long, long ago. For a hash slice, dieing might have been a better choice, but that didn't even occur to me at the time I wrote that patch and it appears nobody since has felt the need to change that behavior.

- tye        

Replies are listed 'Best First'.
Re^3: Scalar context of slice (myth)
by FunkyMonk (Chancellor) on Oct 03, 2008 at 21:32 UTC
    map and grep return lists so, obviously, in a scalar context they also return the last item (according to you). Except they don't, of course.
    Of course they don't. That's why I wrote "map in scalar context returns what you're after: the length of the list. See map."

    I really don't see what point you're trying to make tye :-(

      You said:

      A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar.

      And map and grep each return lists. So your above proclamation would imply that map and grep "will just assign the last item in the list to the scalar". So do you think that map doesn't return a list? Or do you retract your first sentence?

      - tye        

        Hm. Seems to me that a list in a scalar context does return the last item:

        print scalar (1,2,3);; Useless use of a constant in void context at (eval 18) line 1, <STDIN> + line 16. 3
        And map and grep each return lists ...

        ...except, when they find their return context is SCALAR they chose not to return a list, but instead they return a scalar, which happens to be the size of the list they would have returned had they found themselves in a list context.

        ...except also when they find themselves in a void context, in which case they chose not to return anything at all.

        It mostly a matter of words, but I think it is wrong to say "map and grep each return lists", when like any other perl function they can chose whether to return a list, a scalar or nothing at all depending upon the context in which they are called.


        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.