In Operators§
See primary documentation in context for infix andthen.
The andthen
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
.
A handy use of this operator is to alias a routine's return value to $_
and to do additional manipulation with it, such as printing or returning it to caller. Since the andthen
operator short-circuits, statements on the right-hand side won't get executed, unless left-hand side is defined (tip: Failure
s are never defined, so you can handle them with this operator).
"does this match?" ~~ /this/ andthen { .put; my $thing-to-do-if-match-succeeds; ... } # OUTPUT: «this» "does that match?" ~~ /this/ andthen put 'oops'; # NO OUTPUT
The andthen
operator is a close relative of the with
statement modifier, and some compilers compile with
to andthen
, meaning these two lines have equivalent behavior:
.say with 42; 42 andthen .say;