Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #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
in thread Scalar context of slice by thenaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2018-10-24 01:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...














      Results (127 votes). Check out past polls.

      Notices?