rakutruthiness

How does Perl 6 evaluate truthiness?


In reading about Perl 6, I see a feature being trumpeted about, where you no longer have to do:

return "0 but true";

...but can instead do:

return 0 but True;

If that's the case, how does truth work in Perl 6? In Perl 5, it was pretty simple: 0, "", and undef are false, everything else is true.

What are the rules in Perl 6 when it comes to boolean context?


Solution

  • So to combine what I think to be the best of everyone's answers:

    When you evaluate a variable in boolean context, its .true() method gets called. The default .true() method used by an object does a Perl 5-style <0, "", undef> check of the object's value, but when you say "but True" or "but False", this method is overridden with one that doesn't look at the value just returns a constant.

    One could conceivable write a true() method which, say, returned true when the value was even and false when it was odd.