Well, you can always write a method for it at each end. Just nfreeze it to a scalar, figure out how long it is, send a header, then send the object. The reading code needs to read the header, figure out the length of the object, and then read the object.

The trick is getting the reading code to give up if it cannot get everything in the required time. At least two safe ways come to mind to do the trick. Both are a little heavy.

One is to use a select (or IO::Select)/sysread loop to read character by character and die if you fail. If you write this correctly you can actually multiplex multiple loops. But note that you are now writing a Perl level loop for every character coming in - this is not the most efficient thing to do...

The other is to put a process between you and the other end which you don't mind dying. You could launch it with IPC::Open2 with an argument saying how long it is going to live before dying, it sets up the alarm with an exit on read failure (unstable signal handling doesn't matter now since you want it to die), and it will process one object off of the pipe, echo it back to you, then die. You can now do a blocking read of that object, safe in the knowledge that the other end is going away. After it goes away you can reap it with wait or waitpid. Launching a process per object is extreme, but if the objects are very large it may be more efficient.

UPDATE
Response to your update. All that select will do is guarantee a single incoming byte. That is why you have to sysread single chars in a loop. You don't know that there will be more than that, and you don't want to block. And since you have to handle the read at a low-level, you can't punt to Storable's processing method. Which is why I said up front that you would want to write your own communication protocol at each end.


In reply to Re (tilly) 3: Segfault with Storable by tilly
in thread Segfault with Storable by lestrrat

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.