The important thing to understand (and something that I clearly misunderstood ten years ago -- I still remember the feeling of egg on face) is that there simply is no such thing as a list in scalar context. A "list producing expression used in scalar context" does not produce a list. It just produces a single value, which is the last element in the comma-delimited expression. (That's as near as I can come to describing the right-hand-side.)

So the truth is that this construct:

my $a = ( 10, 20, 30, 40, 50 );

...is not generating a list in scalar context and then assigning the last element to $a. It's assigning the scalar value 50 to $a, that's it. There's no list, and the entity has a size of one.

B::Deparse's output is interesting when list-producing syntax is used in scalar context. There's no list. :)

perl -MO=Deparse -E '$x = (10,20,30); say $x'

There you'll see the comma operator at work doing what it's supposed to do in scalar context: throwing away the item on the lefthand, returning the one on the right.


Dave


In reply to Re: Return Value of List-Producing Elements in Scalar Context by davido
in thread Return Value of List-Producing Elements in Scalar Context by pikus

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.