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


In reply to Serializing CODE References by djantzen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.