Beefy Boxes and Bandwidth Generously Provided by pair Networks
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
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found