Documentation for infix orelse

Documentation for infix orelse, assembled from the following types:

language documentation Operators

From Operators

(Operators) infix orelse

Similar to infix //, except with looser precedence and $_ aliasing.

Returns the first defined argument, or else 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 an argument if the right side is a Callable, whose count must be 0 or 1.

This operator is useful to handle Failures returned by routines, since more often than not, the expected value would be defined, while a Failure never is:

sub meows { ++$ < 4 ?? fail 'out of meows!' !! '🐱' }
 
sub meows-processor1 { meows() orelse .return } # return handled Failure 
sub meows-processor2 { meows() orelse fail $_ } # return re-armed Failure 
sub meows-processor3 {
    # Use non-Failure output, or else print a message that stuff's wrong 
    meows() andthen .say orelse something's wrong.say;
}
 
say "{.^name}{.handled}"  # OUTPUT: «Failure, True␤» 
    given meows-processor1;
say "{.^name}{.handled}"  # OUTPUT: «Failure, False␤» 
    given meows-processor2;
meows-processor3;           # OUTPUT: «something's wrong␤» 
meows-processor3;           # OUTPUT: «🐱␤»