… | |
… | |
311 | |
311 | |
312 | =item @states = Coro::State::list |
312 | =item @states = Coro::State::list |
313 | |
313 | |
314 | Returns a list of all states currently allocated. |
314 | Returns a list of all states currently allocated. |
315 | |
315 | |
|
|
316 | =item $clone = $state->clone |
|
|
317 | |
|
|
318 | This exciting method takes a Coro::State object and clones, i.e., creates |
|
|
319 | a copy. This makes it possible to restore a state more than once. |
|
|
320 | |
|
|
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 |
|
|
323 | not supported. |
|
|
324 | |
|
|
325 | Among the games you can play with this is implement a scheme-like call/cc: |
|
|
326 | |
|
|
327 | sub callcc(&@) { |
|
|
328 | my ($f, @arg) = @_; |
|
|
329 | |
|
|
330 | my $c = new Coro::State; |
|
|
331 | |
|
|
332 | my $F; $F = new Coro::State sub { |
|
|
333 | $f->( |
|
|
334 | sub { |
|
|
335 | @arg = @_; |
|
|
336 | $F->transfer ($c->clone); |
|
|
337 | }, |
|
|
338 | @arg |
|
|
339 | ); |
|
|
340 | }; |
|
|
341 | |
|
|
342 | $c->transfer ($F); |
|
|
343 | @arg |
|
|
344 | } |
|
|
345 | |
|
|
346 | Here are a few little-known facts: First, coroutines *are* full/true/real |
|
|
347 | continuations. Secondly Coro::State objects (without clone) *are* first |
|
|
348 | class continuations. Thirdly, nobody has ever found a use for the full |
|
|
349 | power of call/cc that isn't better (faster, easier, more efficient) |
|
|
350 | implemented differently, and nobody has yet found a useful control |
|
|
351 | construct that can't be implemented without it already, just much faster |
|
|
352 | and with fewer resources. |
|
|
353 | |
|
|
354 | Besides, call/cc is much less useful in a Perl-like dynamic language (with |
|
|
355 | references, and its scoping rules) then in, say, scheme. |
|
|
356 | |
|
|
357 | Now, limitations of clone: |
|
|
358 | |
|
|
359 | 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), |
|
|
361 | and C<abort ()> is the preferred mechanism to signal errors. It probbaly |
|
|
362 | also leaks, and sometimes triggers a few assertions inside Coro. Most of |
|
|
363 | these limitations *are* fixable with some effort, but that's pointless |
|
|
364 | just to make a point that it could be done. |
|
|
365 | |
316 | =cut |
366 | =cut |
317 | |
367 | |
318 | # used by Coro::Debug only atm. |
368 | # used by Coro::Debug only atm. |
319 | sub debug_desc { |
369 | sub debug_desc { |
320 | $_[0]{desc} |
370 | $_[0]{desc} |