In a scalar context, a list evaluates to the number of elements it contains.

More precisely, in a scalar context a list assignment returns the number of items on the right-hand side.

If anyone ever says of Perl "a list in a scalar context", then you know they've fallen victim to a very common misconception. They'll usually end that with "returns the last item in the list", but that wouldn't have made sense in this case.

The fact is that there is no one rule for what you get when you "put a list in a scalar context". Some would even say that you can't put a list into a scalar context. I prefer to say that it depends on what the "list" is. Because I don't feel that "list" is a well-defined term in Perl.

There are a great many "things that would return a list if used in a list context" and each of them can have their own rule for what they return if used in a scalar context. The most common is "last item", though exactly what an "item" is can be a bit tricky to define. (:

                - tye

In reply to Re^2: undef var ("list in scalar context") by tye
in thread undef var by aapada

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.