Thanks, merlyn++.

Note that in the following cases, "last element" doesn't mean the same thing each time.

(10, 20, 30)last element
@foo[3..5]last element
(10, 20, 30)[2, 1]last element

I'd actually change one entry:

(10, 20, 30)last expression

Because it isn't the "last element" of the "list of scalar values that this operation would produce if called in a list context" but is instead the "last element" in the "list of Perl expressions that are separated by commas". Likewise, I might change the other two to "last scalar value" to be clearer.

This is another perfect example of why "list" doesn't just mean one thing when talking about Perl. Each of those cases return the "last element" of some "list", it is just that "list" means two different things in those two cases.

I think this is a big part of why people get confused and people argue when talking about "lists" in Perl. Since "list context", "scalar context", and "scalar" all have precise definitions in Perl, it is natural to think that "list" has a precise definition as well.

#!/usr/bin/perl -wl use strict; my @idx= ( 2, 99, 5 ); my @a2z= ( 'a'..'z' ); my @arr= ( 10..16 ); print '(@arr,@a2z): ', scalar( (@arr,@a2z) ); print '(@arr,@a2z)[@idx]: ', scalar( (@arr,@a2z)[@idx] ); print '@arr[@idx]: ', scalar( @arr[@idx] );


Useless use of private array in void context at line 9. (@arr,@a2z): 26 (@arr,@a2z)[@idx]: 15 @arr[@idx]: 15

So only the first item is lazy (passing the scalar context inside such that a "list of scalar values on the stack" doesn't get generated just to be mostly thrown away).

I still believe that the other two "should" have been lazy (pass their scalar context into @idx) but instead the developers of Perl were lazy and just wrote code to generate the list of scalar values and simply pick the last one at the last minute. But since slices in scalar context are more "edge" or "corner" cases, being lazy with the design was probably the right choice. (:

So $s = (10,20,30) does contain "a list in scalar context". Either one has to admit that or one has to not say that $s gets set the the "last element of the list". :)

- tye        

In reply to Re: On Scalar Context ("list") by tye
in thread On Scalar Context by merlyn

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.