BUU has asked for the wisdom of the Perl Monks concerning the following question:

I was having an argument on IRC over exactly what happened to a list in boolean context. My opponents were arguing that an empty list in boolean context is false, where as I was arguing that a list can't exist in scalar context. However then they brought several one liners that seemed to demonstrate empty lists being false. I wasn't sure exactly what was going on, but I was dead certain that I was right. So I come here. Anyone know exactly what happens with empty lists and boolean context?

Below are several one liners that seem to demonstrate empty lists and such:
$ perl -wle'print defined +() ? 1 : 0' $ 0 $ perl -wle'$_ = 1; print defined +() ? 1 : 0' $ 0 $ perl -wle'$_ = 1; print defined () ? 1 : 0' $ 1

Replies are listed 'Best First'.
Re: An empty list in boolean context?
by herveus (Prior) on Aug 05, 2005 at 18:12 UTC
    Howdy!

    Boolean context is a species of a scalar context. Thus, consider what happens to a list when it is placed into a scalar context.

    $ perl -e 'if (()) { print "yes" } else { print "no" }' no

    yours,
    Michael
Re: An empty list in boolean context?
by Joost (Canon) on Aug 05, 2005 at 19:05 UTC
    A literal list in scalar context evaluates to its last element. An empty literal list in scalar context evaluates to undef, which is false.

    Beware: in fact, a list in perl is only a syntactic construct - it doesn't really exist - as far as any code is concerned, you only deal with arrays, hashes and scalars.

    update: oh yes, and typeglobs. :-)

Re: An empty list in boolean context?
by revdiablo (Prior) on Aug 05, 2005 at 20:37 UTC

    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.

Re: An empty list in boolean context?
by merlyn (Sage) on Aug 06, 2005 at 02:55 UTC
    a list in boolean context
    That's like saying "when a pig flies". It cannot exist. Parens do not make a list in a scalar context. Nothing can make a list in a scalar context. Hence, your question is already unanswerable, since it presumes things that cannot exist.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: An empty list in boolean context?
by sk (Curate) on Aug 05, 2005 at 18:29 UTC
    The return value from an empty list when used in a scalar context is undef.

    so why should the condition succeed?

    perl -e '$empty = (); print ("hi there\n") if \!$empty; __END__ hi there

    perl -e '$empty = (); print ("hi there\n") if \!defined($empty);' __END__ hi there

    PS: I am using \! to avoid my csh from interepreting it as a special shell character.

Re: An empty list in boolean context?
by ikegami (Patriarch) on Aug 05, 2005 at 18:11 UTC

    I presume you added the third case to show that the value of $_ has no bearing on the first two cases. Here's that fact illustrated a little better:

    >perl -MO=Deparse -wle "print defined +() ? 1 : 0" print defined(()) ? 1 : 0; >perl -MO=Deparse -wle "$_ = 1; print defined +() ? 1 : 0" $_ = 1; print defined(()) ? 1 : 0; >perl -MO=Deparse -wle "$_ = 1; print defined () ? 1 : 0" $_ = 1; print defined $_ ? 1 : 0;

    Update: Why are you checking for defined? define accepts a scalar, not specifically a boolean. False is defined, so your tests should all print 1 if you were truly examining lists in boolean contexts. (False is 0 in numerical context and "" in string context.) To check the value of a list in a boolean context, simply run the following:

    >perl -wle "print(()?'True':'False');" False

    I think your question is invalid. Just like there's no such thing as a list in scalar context, I'm guessing that there is no such thing as a list in a boolean context. It's up to the functions returning lists to provide the value.