ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
(Generate patch)

Comparing Coro/Coro/State.pm (file contents):
Revision 1.126 by root, Thu Nov 20 09:37:21 2008 UTC vs.
Revision 1.127 by root, Sat Nov 22 05:13:34 2008 UTC

311 311
312=item @states = Coro::State::list 312=item @states = Coro::State::list
313 313
314Returns a list of all states currently allocated. 314Returns a list of all states currently allocated.
315 315
316=item $clone = $state->clone
317
318This exciting method takes a Coro::State object and clones, i.e., creates
319a copy. This makes it possible to restore a state more than once.
320
321Since its only known purpose is intellectual self-gratification, and
322because it is a difficult piece of code, it is not enabled by default, and
323not supported.
324
325Among 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
346Here are a few little-known facts: First, coroutines *are* full/true/real
347continuations. Secondly Coro::State objects (without clone) *are* first
348class continuations. Thirdly, nobody has ever found a use for the full
349power of call/cc that isn't better (faster, easier, more efficient)
350implemented differently, and nobody has yet found a useful control
351construct that can't be implemented without it already, just much faster
352and with fewer resources.
353
354Besides, call/cc is much less useful in a Perl-like dynamic language (with
355references, and its scoping rules) then in, say, scheme.
356
357Now, limitations of clone:
358
359It probably only works on perl 5.10; it cannot clone a coroutine inside
360the substition opertaor (but windows perl can't fork from there either),
361and C<abort ()> is the preferred mechanism to signal errors. It probbaly
362also leaks, and sometimes triggers a few assertions inside Coro. Most of
363these limitations *are* fixable with some effort, but that's pointless
364just 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.
319sub debug_desc { 369sub debug_desc {
320 $_[0]{desc} 370 $_[0]{desc}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines