You've left out an important case from the table: regular expressions.

Here's how they work wrt contexts (correct me where I'm wrong).

Regexps without the /g flag return true/false in saclar context (the list of captures or 1 if there are no captures, empty list when does not match).

Regexps with the /g flag match only once in scalar (or void) context and return true/false. (In list context, they match as many times as possible.)

Substitutions (s///) without the /g flag return true/false in either context, wit the g flag they return the number of successful matches in either context.

Also missing are the .. and ... operators which do quite different thing in scalar context than in list context.

List assignments (of form (LLIST)=EXPR) return the length of the right hand side.

As for your claim that less expressions return the length than not, it was quite obvious for me, as most expressions always return a 1 long list and the same one element in scalar context, all arithmetic operators and also the and, or operators work like this.

(Also there's this interesting issue about what a substr-assignment returns, but it's the same in scalar and list contexts, so it's unrelated to this thread.)

In reply to Re: On Scalar Context by ambrus
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.