return
является CONTROL
исключением
-> {
return 42
}();
CONTROL {
default {
.^name.say; # CX::Return
}
}
Вы можете завернуть блок во что-то, что имеет блок CONTROL
, или в то, что уже обрабатывает CX::Return
, как sub
my &c = ->{
return 42
}
sub {
c(); # call it
say 'never gets here';
}().say; # call it and say the result of `return`
Я думаю, next
было бы разумнее использовать на кране.
$supply.tap: -> $message {
next unless server-is-alive( );
send-to-server( $message );
}
Это в настоящее время не работает.
В любом случае, почему вы не используете более приятную функцию [react
| supply
] / whenever
?
(Который работает с next
/ last
)
react whenever $supply -> $message {
next unless server-is-alive( );
send-to-server( $message );
}
Обратите внимание, что он заблокирует текущий поток, чтобы получить его, чтобы он не добавил start
впереди.
Тестовый код:
# setup some messages
my $supply = Supply.interval(0.1).map: {
.Str.uninames.join(' ' x 4);
}
react {
my $server-is-alive = True;
sub server-is-alive (){ $server-is-alive }
sub send-to-server ( $message ){
say $message
}
whenever Supply.interval( 0.5 ) {
$server-is-alive = !$server-is-alive;
say "server is { 'not ' x !$server-is-alive }alive";
}
# here is your code
whenever $supply -> $message {
next unless server-is-alive( );
send-to-server( $message );
}
whenever Promise.in(3) {
done
}
}
В результате
server is not alive
server is alive
DIGIT FIVE
DIGIT SIX
DIGIT SEVEN
DIGIT EIGHT
DIGIT NINE
server is not alive
server is alive
DIGIT ONE DIGIT FIVE
DIGIT ONE DIGIT SIX
DIGIT ONE DIGIT SEVEN
DIGIT ONE DIGIT EIGHT
DIGIT ONE DIGIT NINE
server is not alive
server is alive
DIGIT TWO DIGIT FIVE
DIGIT TWO DIGIT SIX
DIGIT TWO DIGIT SEVEN
DIGIT TWO DIGIT EIGHT
DIGIT TWO DIGIT NINE
server is not alive