In Operators§
See primary documentation in context for infix notandthen
The notandthen
operator returns Empty
upon encountering the first defined argument, otherwise the last argument. Last argument is returned as-is, without being checked for definedness at all. Short-circuits. The result of the left side is bound to $_
for the right side, or passed as arguments if the right side is a Callable
, whose count must be 0
or 1
.
At first glance, notandthen might appear to be the same thing as the orelse operator. The difference is subtle: notandthen returns Empty
when it encounters a defined item (that isn't the last item), whereas orelse returns that item. In other words, notandthen is a means to act when items aren't defined, whereas orelse is a means to obtain the first defined item:
sub all-sensors-down { [notandthen] |@_, True } sub first-working-sensor { [orelse] |@_, 'default sensor' } all-sensors-down Nil, Nil, Nil and say 'OMG! All sensors are down!'; # OUTPUT:«OMG! All sensors are down!» say first-working-sensor Nil, Nil, Nil; # OUTPUT:«default sensor» all-sensors-down Nil, 42, Nil and say 'OMG! All sensors are down!'; # No output say first-working-sensor Nil, 42, Nil; # OUTPUT:«42»
The notandthen
operator is a close relative of the without
statement modifier, and some compilers compile without
to notandthen
, meaning these two lines have equivalent behavior:
sub good-things { fail } 'boo'.say without good-things; good-things() notandthen 'boo'.say;