The series “Yet Another Perl 6 Operator” is back with this brand new article
In the article on coercion operators, we got to know the prefix operator '?' which converts values into Bool::True or Bool::False. Like it happens with '~' for strings, '?' is recurrent for boolean operators.
In Perl 6, the usual infix boolean operators are:
?& - and ?| - or ?^ - xor
These operators evaluate their operands in boolean context and apply simple Boole’s algebra on them.
False ?& False # False '' ?& 'yes' # False 1 ?& False # False 42 ?& 42 # True"" ?| 0 # False False ?| True # True [1] ?| 0 # True True ?| True # True'' ?^ '' # False undef ?^ {a=>1} # True {:k} ?^ undef # True True ?^ True # False
Each of the three operators always evaluate both sides and return one of the standard values Bool::True or Bool::False. So these boolean AND and OR do not short-circuit as their logical counterparts: '&&' and '||'. Precedence is different too.
Equivalent to precedence$a ?& $b ?$a * ?$b != 0 multiplicative $a ?| $b ?$a + ?$b != 0 additive $a ?^ $b ?$a + ?$b == 1 additive
The boolean negation operator may be written '?^' or '!'.
?^ $a Equivalent to True ?^ $a
Conceptually '?^' coerces to boolean first and then flips the bit. Synopsis 3 recommends the use of '!' instead.
Update: rhr at #perl6@freenode pointed how I mistakenly took [] and {} to be true in the examples above. In Perl 6, containers and hashes are true only if they have a least one member or pair, respectively. So I got it right.
After a too long pause, I am resuming the series to the delight of Perl 6 lovers and haters (though I don’t think they exist… the haters :).
Next article will be due… soon. (Not joking.)
LINKS
- Synopsis S03, the official source
- The introduction of this series
- Official Perl 6 Documentation
- Perl 6 in your browser


Glad to see the series continue.