See Original text in context
The notandthen
operator returns Empty
upon encountering the first undefined 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 undefined 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-downsub first-working-sensorall-sensors-down Nil, Nil, Niland 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, Niland say 'OMG! All sensors are down!'; # No outputsay first-working-sensor Nil, 42, Nil; # OUTPUT:«42»
The notandthen
operator is a close relative of with
statement modifier, and some compilers compile without
to notandthen
, meaning these two lines have equivalent behavior:
sub good-things'boo'.say without good-things;good-things() notandthen 'boo'.say;