Problems? Is your data what you think it is? | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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 In reply to Re^2: Scalar context of slice (myth)
by tye
|
|