Contize - Help an object be a continuation thingie (suspendable)
# Contize an object my $obj = new Contize(new Object);
# An output-and-suspend method sub output { my ($self, $msg) = @_; print '<html><body><form method=post>' print $msg; print '</form></body></html>'; $self->suspend; }
# An input method sub input { my ($self, $msg) = @_; $self->output( $msg . '<input type="text" name="response">' ); $q = new CGI; return $q->param('response'); }
# Now the magical bit! sub addTwo { my $self = shift; my $a = $self->input("Enter first number:"); my $b = $self->input("Enter second number:"); $self->output("Total of \$a + \$b = " . ($a + $b)); }
# This example would be completed with a wrapper script to save/restore the # object to disk between runs. See the WebGuess example
Contize is primarily meant to be useful in the context of CGI programming. It effectively alters the programmer's view of what is happening -- changing it from a program which is run and re-run with each input/output into a program which is continuously run, sending output and then pausing for input at certain intervals. Documentation on using Contize for this style of CGI programming can be found elsewhere, the remainder of this documentation will be more directly on Contize (and who knows... maybe there is some other use for Contize of which I haven't thought).
Contize helps an object to be suspendable and resumeable. For this to happen the object must be Contized, which is a lot like being blessed or Memoized. Once an object has been Contized several new methods are provided to it. The two most important methods are suspend and resume.
The suspend method logically replaces the normal return statement. So instead of a method returning its results directly it instead does "$self->suspend(@results)". The suspend method contains an 'exit', so upon suspend the entire process is terminated. In order to succesfully be resumed at a later point, the owner of this object should have an END block which saves the Contized object to long-term storage.
The resume method is called by the program after it has restored the Contized object from long-term storage. This restores the objects internal state so that subsequent calls to its methods will (more or less) pick up right where they left off. So, if you have a CGI::Session object for example, you might have something like this:
my $obj = $session->param('obj') || new Contize(new MyObj); $obj->resume(); $obj->run();
Fun, eh?
Takes a $thingie object and continuizes it... we replace it with ourselves and intercept all method calls.
Note that we take over the following elements of the hash:
So you probably should't use these as variables in the real object.
Turn off caching for the given methods
AUTOLOAD actually does the work. We intercept method invocations and usually cache the results. Difficult to explain...
This replaces the return function in a subroutine and suspends the object. When the object is resumed it will give $retval to the caller.
Reset the thingie so that it will be re-run. This clears the callstack and the callstack_count so that it will begin returning cached results.
Upon destruction we undef our child, thus calling the child's own DESTROY, if such a thing exists. I'm pretty sure this is the proper way to do things, but it might break if their DESTROY does more complicated activities.
Contize has quite a bit of overhead for internal caching of method invocations.
There should be a bit more documentation here on how Contize actuall works.
Contize will only work on objects which use a hash as their core thingie.
Coro::Cont, http://thelackthereof.org/wiki.pl/Contize
Brock Wilcox <awwaiid@thelackthereof.org> http://thelackthereof.org/
Copyright (c) 2004 Brock Wilcox <awwaiid@thelackthereof.org>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.