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

return 0 is not false in list context! It suffers from the same problem as return undef described elsewhere in this thread. And ...==0 is not how you check for falsehood in Perl! If you really, really wanted to explicitly return a false value that is not undef and avoid the above problem, you'd have to write "return wantarray?():!1". But since you're writing a function that is evaluated in boolean context, that's unnecessary, since undef is just as false. So I suggest that if you don't like the looks of "return;", write "return; # return false" instead.

Replies are listed 'Best First'.
Re^3: What could make "()" a good value for boolean false?
by ikegami (Patriarch) on Mar 31, 2016 at 03:02 UTC

    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.

      …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.

      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.
      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.
Re^3: What could make "()" a good value for boolean false?
by Marshall (Canon) on Mar 29, 2016 at 08:18 UTC
    This list context idea doesn't appear to have anything to do with the requirements for a "hook" for this terminal emulator. The hook returns something that is evaluated in a scalar context. When the hook fails, it should: return 1;. The suggestion for when the hook succeeds is to end the sub with simply (), instead of even a return! Ok, if that is what you want to do, then I would agree that "return;" is better than a simple "()". Perl will return the value of the last statement in a sub if there is no explicit return. I argue that return (0); is better than a simple () or bare "return;" when the objective is to return a simple scalar true/false.

    One of the pages that talks about this: Hooks. You can see some examples of code at Example Hook Code. It is clear to me that the intent is a simple true/false that will be evaluated in a scalar, not list context.

      I argue that return (0); is better than a simple () or bare "return;"

      Why? What would the advantage of that be?? The disadvantage has been discussed in this thread.

      One of the pages that talks about this: Hooks.

      Sorry, but that page only contradicts your argument. Nowhere is a guarantee made as to which context the hooks are called in (the documentation of the callback on_osc_seq even explicitly says "The default should be to return an empty list."). The page explicitly says "All of these hooks must return a boolean value. ... When in doubt, return a false value (preferably ())." As per Truth and Falsehood, the false values in Perl are: "The number 0, the strings '0' and "", the empty list (), and undef". A list with one element is true, and so again, return 0 is not always false!

      Further I argue that your interpretation of the intent is wrong. If the author of the documentation wanted you to return 1 or 0, or if the hooks were always evaluated in scalar context, he/she would have said so. By deliberately telling users to return the empty list (), they are, for example, keeping the door open for the hooks API to be more easily extended in the future - hooks could return multiple values.