djantzen has asked for the wisdom of the Perl Monks concerning the following question:
Some time ago I wrote a set of modules to handle future events in a Web publishing system. The idea was to create an object that would represent an action, call it a ‘Job’, associate a timespec with it for when it should be executed, freeze it using the Storable module, and then write it to an Oracle CLOB (Character Large Object) column. When the specified time rolled around, the object would be revived, and the instructions contained therein executed.
The current system works okay, but not great, and the recent discussions of closures here (What's a closure?,Why are closures cool?) got me thinking that what I really want to do is to serialize a closure for execution at a later time. (Actually, in most cases a proper closure won’t be necessary, a normal CODE reference will suffice.) My hopes were dashed however, as I discovered in my test code that Storable refuses to work with CODE references, and sure enough the documentation says:
You can’t store GLOB, CODE, FORMLINE, etc... If you can define seman +tics for those operations, feel free to enhance Storable so that it can deal with them.
So, short of learning XS and hacking those 4,000 or so lines of C underneath Storable, the closest I can get to this is to store the subroutine in plain text and eval it later. As in:
my $code = q( sub { my ($x, $y); doSomeStuff($x, $y); } ); my $sub = eval ($code);
This will do the trick for me most of the time, however I’d like to hear some thoughts on the issues and difficulties in defining a semantics for serializing CODE references. It is evident to me now that serializing proper closures would be insanely difficult, and indeed futile given that the point of serialization is to preserve a data structure between processes, making it a “pass-by-value” activity, and closures involve references to lexical variables from the scope within which the subroutine is defined. However, I don’t yet see why generating a semantics for normal CODE refs would be so difficult.
Thanks for you thoughts, fever
Edit by tye to replace PRE around long lines with CODE
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Serializing CODE References
by gellyfish (Monsignor) on Feb 06, 2002 at 12:35 UTC | |
|
Re: Serializing CODE References
by dreadpiratepeter (Priest) on Feb 06, 2002 at 15:04 UTC |