… | |
… | |
320 | |
320 | |
321 | Since its only known purpose is intellectual self-gratification, and |
321 | Since its only known purpose is intellectual self-gratification, and |
322 | because it is a difficult piece of code, it is not enabled by default, and |
322 | because it is a difficult piece of code, it is not enabled by default, and |
323 | not supported. |
323 | not supported. |
324 | |
324 | |
325 | Among the games you can play with this is implement a scheme-like call/cc: |
325 | Among the games you can play with this is implement a scheme-like call/cc, |
|
|
326 | as the following code does (well, with small differences). |
326 | |
327 | |
327 | sub callcc(&@) { |
328 | sub callcc(&@) { |
328 | my ($f, @arg) = @_; |
329 | my ($func, @arg) = @_; |
329 | |
330 | |
330 | my $c = new Coro::State; |
331 | my $state = new Coro::State; |
331 | |
|
|
332 | my $F; $F = new Coro::State sub { |
332 | my $wrapper; $wrapper = new Coro::State sub { |
333 | $f->( |
333 | my $escape = sub { |
334 | sub { |
|
|
335 | @arg = @_; |
334 | @arg = @_; |
336 | $F->transfer ($c->clone); |
335 | $wrapper->transfer ($state->clone); |
337 | }, |
|
|
338 | @arg |
|
|
339 | ); |
336 | }; |
|
|
337 | $escape->($func->($escape, @arg)); |
340 | }; |
338 | }; |
341 | |
339 | |
342 | $c->transfer ($F); |
340 | $state->transfer ($wrapper); |
343 | @arg |
341 | @arg |
344 | } |
342 | } |
345 | |
343 | |
346 | Here are a few little-known facts: First, coroutines *are* full/true/real |
344 | Here are a few little-known facts: First, coroutines *are* full/true/real |
347 | continuations. Secondly Coro::State objects (without clone) *are* first |
345 | continuations. Secondly Coro::State objects (without clone) *are* first |
… | |
… | |
355 | references, and its scoping rules) then in, say, scheme. |
353 | references, and its scoping rules) then in, say, scheme. |
356 | |
354 | |
357 | Now, limitations of clone: |
355 | Now, limitations of clone: |
358 | |
356 | |
359 | It probably only works on perl 5.10; it cannot clone a coroutine inside |
357 | It probably only works on perl 5.10; it cannot clone a coroutine inside |
360 | the substition opertaor (but windows perl can't fork from there either), |
358 | the substition operator (but windows perl can't fork from there either) |
361 | and C<abort ()> is the preferred mechanism to signal errors. It probbaly |
359 | and some other contexts, and C<abort ()> is the preferred mechanism to |
|
|
360 | signal errors. It cannot clone a state that has a c context attached |
|
|
361 | (implementing clone on the C level is too hard for me to even try), which |
|
|
362 | rules out calling call/cc from the main coroutine. It probably also |
362 | also leaks, and sometimes triggers a few assertions inside Coro. Most of |
363 | leaks, and sometimes triggers a few assertions inside Coro. Most of these |
363 | these limitations *are* fixable with some effort, but that's pointless |
364 | limitations *are* fixable with some effort, but that's pointless just to |
364 | just to make a point that it could be done. |
365 | make a point that it could be done. |
365 | |
366 | |
366 | =cut |
367 | =cut |
367 | |
368 | |
368 | # used by Coro::Debug only atm. |
369 | # used by Coro::Debug only atm. |
369 | sub debug_desc { |
370 | sub debug_desc { |
… | |
… | |
387 | if $current; |
388 | if $current; |
388 | |
389 | |
389 | _set_current $main; |
390 | _set_current $main; |
390 | } |
391 | } |
391 | |
392 | |
392 | use Carp; warn Carp::cluck;#d# |
|
|
393 | |
|
|
394 | 1; |
393 | 1; |
395 | |
394 | |
396 | =back |
395 | =back |
397 | |
396 | |
398 | =head1 BUGS |
397 | =head1 BUGS |