orelse
ErrorsCollection

orelse

Synthesised documentation from language/operators

From language/operators

See Original text in context

The orelse operator is 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 for handling Failures returned by routines since the expected value is usually undefined and Failures 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: «🐱␤»