A highly experimental implementation of coroutines has landed to Pugs. The `coro` keyword denotes a coroutine. It may appear at any place where `sub` may appear, i.e. in both named and anonymous forms.
I borrowed the semantics from Coro::Cont on CPAN, with the following restriction that you cannot return() from a coroutine, or yield() from a non-coroutine. Tests for coro should go to t/unspecced/, as one of the "speculative" features. Use it like this: coro flip_flop { yield 1; yield 0 } print flip_flop() while 1; # 1010101010.... As you can see, there is an implicit loop around coro's body. state() and my() variables work as you would expect -- the former stays in place, while the latter are regenerated when control falls out from the block and reenters from the top. Here is another example, courtesy of integral: my @generators = map -> $N { coro { yield $_ for 0..$N } } 0..9; say "{ map { $_() }, @generators }" for 0..9; The above prints: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 2 2 2 2 2 2 2 2 0 1 0 3 3 3 3 3 3 3 0 0 1 0 4 4 4 4 4 4 0 1 2 1 0 5 5 5 5 5 0 0 0 2 1 0 6 6 6 6 0 1 1 3 2 1 0 7 7 7 0 0 2 0 3 2 1 0 8 8 0 1 0 1 4 3 2 1 0 9 Please direct syntax-related questions and suggestions to p6l as separate threads; I'd be happy to change the implementation in other ways as specified by @Larry. Thanks, /Autrijus/
pgpIZMrkSGnzS.pgp
Description: PGP signature