Logical operators
FullShort-circuiting and / or / not, defined-or //, and their low-precedence words.
Raku's logical operators short-circuit and, crucially, return one of their operands rather than a plain Bool — so they double as value-selectors. There are tight symbolic forms and loose word forms.
and, or, not #
&& is logical-and, || logical-or, ! negation. In Boolean context they behave as expected.
say True && False; say True || False; say !True;
False
True
FalseThey return an operand #
|| returns the first true operand (or the last), && the first false one (or the last) — the basis of the "or a default" idiom.
say 0 || "default"; say "" || "fallback"; say 42 && "reached";
default
fallback
reachedDefined-or — // #
// returns its left side unless it is undefined, in which case the right. Unlike ||, a defined-but-false value (like 0) passes through.
say Any // "fallback"; say 5 // 10;
fallback
5Low-precedence words #
and, or, not are the same operations at very loose precedence — handy for control flow (open($f) or die) without parentheses.
say (1 and 2); say (0 or 3); say (not 0);
2
3
TrueNotes #
- Because
//keys on definedness, use it for "default when missing" and||for "default when falsy" —0 // 9is0, but0 || 9is9. - The word forms sit below assignment in precedence, so
$x = $a or $bparses as($x = $a) or $b— a classic gotcha; use||when you mean the value. - Each has an assignment metaform:
||=,&&=,//=(assign only if the current value is false / true / undefined).