In Perl, () is a grouping construct. It does not create anything, it simply disambiguates things for the parser. Further, a list only exists in list context. There can not, by definition, be a list in scalar context. It would be nice if there was some more canonical documentation, but the best explanation I found is in perlfaq4, under the question "What is the difference between a list and an array?":

As a side note, there's no such thing as a list in scalar context. When you say

$scalar = (2, 5, 7, 9);

you're using the comma operator in scalar context, so it uses the scalar comma operator. There never was a list there at all! This causes the last value to be returned: 9.

Update: digging further, I found the following in the "Truth and Falsehood" section of perlsyn:

The number 0, the strings '0' and '', the empty list "()", and "undef" are all false in a boolean context.

I wonder if () is a special case that always creates an empty list (if this is the case, then the earlier quoted section must be wrong in at least one case), or if this is just one of those simplifying lies that makes life so much easier?

Another update: just for the record, I'm of the mind that the "Truth and Falsehood" quote is a one of those lies that we use to make explaining something much easier. I'm leaning towards thinking of the () here as an empty statement -- and not a list at all -- which evaluates to false.


In reply to Re: An empty list in boolean context? by revdiablo
in thread An empty list in boolean context? by BUU

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.