in reply to Re^2: What could make "()" a good value for boolean false?
in thread What could make "()" a good value for boolean false?

A list with one element is true

Nope. It's impossible to evaluate whether it's true or false.

return 0 is not false in list context!

It's not true either. The concept of truth of something in list context makes no sense.

  • Comment on Re^3: What could make "()" a good value for boolean false?

Replies are listed 'Best First'.
Re^4: What could make "()" a good value for boolean false?
by Your Mother (Archbishop) on Mar 31, 2016 at 03:54 UTC

    …Maybe this is what is meant?

    perl -le 'sub bingo { return 0 }; @a = bingo(); print @a ? "True" : "F +alse"' True

      Yes (although strictly speaking, that's an array in scalar context), and also

      $ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar"; return 0; }; print( (()=foo()) ? "true" : "false" );' list true

      (although strictly speaking, that's a list assignment in scalar context). But there's also perlsyn (emphasis mine):

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

        That doesn't check if () is true or false. Change it to ($x) or @a and you'll get exactly the same result. That check if = is true or false, which check the number of elements on the **RHS** of =.

        That passage is poorly worded, leading to people like you quoting it as it says something other than it says. When it says (), when it says "()", it's actually referring to "the value returned by () in scalar context, which is the same thing as saying undef. () is not a value. It shouldn't list one of the many many operator that can return something false in scalar context.

Re^4: What could make "()" a good value for boolean false?
by Anonymous Monk on Mar 31, 2016 at 08:39 UTC

    Okay, I'll concede this one:

    $ perl -wMstrict -le 'if((2,1,0)) {print"true"}else{print"false"}' Useless use of a constant (2) in void context at -e line 1. false

    Although that's the comma operator operating in scalar context, (2,1,0) might be what people might think of when they think "list". But:

    $ perl -wMstrict -le 'if(()=(2,1,0)) {print"true"}else{print"false"}' true

    although admittedly that's the result of a list assignment in scalar context.

    In this thread we're talking about return, and I haven't yet found a way for a return 0; in some kind of list context to be evaluated as false:

    $ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar"; return (0); }; if(foo) {print "true"} else {print "false"} ' scalar false $ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar"; return (0); }; if(()=foo) {print "true"} else {print "false"} ' list true $ perl -wMstrict -le 'sub foo { print wantarray?"list":"scalar"; return (0); }; my @x=foo;if(@x) {print "true"} else {print "false"}' list true

    Although those last two are respectively, a list assignment and an array, not exactly a list.

    So in the above examples, it's never really the lists which are directly in boolean context, so I kind of get what you are saying. But then again, the way I see it conceptually, in Perl, when you evaluate lists/arrays in a scalar context they evaluate to one of two things: their last element (although that one is usually the result of the comma operator in scalar context), or a count of their elements - and at least in my mind that's the underlying principle why a list with >0 elements is always "true". I haven't yet been able to come up with a counterexample where a list with at least one element somehow fiddled into a boolean context evaluates to false; if you've got one, I don't mind being proven wrong.

    For my final argument, perlsyn, emphasis mine:

    The number 0, the strings '0' and "", the empty list (), and undef are all false in a boolean context. All other values are true.
      That passage is poorly worded, leading to people like you quoting it as it says something other than it says. It's actually referring to the value returned by () in scalar context, which is the same thing as saying undef. () is not a value. It shouldn't list one of the many many operator that can return something false in scalar context.

        After also having consulted the Camel I now see your point and that you are right. Thank you for your patience and the lesson.

Re^4: What could make "()" a good value for boolean false?
by morgon (Priest) on Mar 31, 2016 at 07:24 UTC
    The concept of truth of something in list context makes no sense.
    As fas as I understand it truth - as fas as perl is concerned - is the result of evaluating something in boolean context.

    In boolean context an array evaluates just like in scalar context to it's length.

    Therefore I would claim that the statement above ("A list with one element is true") is actually correct.

      I agree that an array with one element is true, but arrays and lists are different things.