Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 453
raldi Avatar asked Sep 24 '08 03:09

raldi


1 Answers

Perl 6 evaluates truth now by asking the object a question instead of looking at its value. The value is not the object. It's something I've liked about other object languages and will be glad to have in Perl: I get to decide how the object responds and can mutate that. As ysth said, you could do that in Perl 5 with overload, but I always feel like I have to wash my hands after doing it that way. :)

If you don't do anything to change that, Perl 6 behaves in the same way as Perl 5 so you get the least amount of surprise.

like image 147
brian d foy Avatar answered Sep 20 '22 06:09

brian d foy