In the case of grep, you're not assigning a list to anything. What you're assigning to the scalar is the return value from grep, which will be a list in list context and a scalar in scalar context.

s/grep/a slice/g above and it all is precisely as true as it was before the substitution.

I fail to see how the scalar return value from grep when called in scalar context is equivalent to the list returned by a slice.

Swap "grep" with "a slice" with each other above and it is just as true: I fail to see how the scalar return value from a slice when called in scalar context is equivalent to the list returned by grep.

To be fair to FunkyMonk, there was nothing said about everything that can in any circumstance always returning the list last item of a list it took as an argument in scalar context.

Yes, but s/said/implied/ and then you'd be dead wrong. What was said was that "X returns Y and Y does Z" as explanation of for why "X does Z". That only makes logical sense if "X returns Y" implies that "X does Z" which only makes sense if the "Y does Z" phrase really meant "Y always does Z" or "every Y does Z".

And it makes even less sense when this is adjacent to the mention of how W if different and it is pretty clear that "W returns Y" in exactly the same sense as "X returns Y".

You seem to hold the mistaken impression that a slice (any of the three types of slice) is fundamentally different from grep and map. Each type of slice only returns a list when in list context and each type of slice makes a(n independent) choice about what to return when in scalar context, exactly like grep and map. (And the optimization done to map and/or grep with Perl 5.010 certainly has nothing to do with what anything chooses to return in scalar context.)

- tye        


In reply to Re^8: Scalar context of slice (vs grep) 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, details, 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, summary, 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.