I've created an environment that allows people to write "plugins" (really just Perl subs) that are registered, then run in a certain order, with various framework features provided. One of those features is error handling, so plugins can register Perl subs which are pushed onto a list, and if some subsequent plugin fails, the error handler subs are invoked to "clean up".

This all works great and I catch various signals for sanity. But of course I cannot catch kill -9 or a power failure, so now I'm trying to see if I can recover after these kinds of events. My basic idea is to use Dumper() to write out a file containing information about the state of the processing, then when I start up and notice one of these variable cache files I can read it and see what was going on. And one of the important items to remember are these error handler subs.

Up until now I've been storing sub refs, like: my::Error::register(\&handler);

That doesn't work well with Dumper() of course. So I was going to change the my::Error::register() sub to take a string name of the handler instead of a sub ref. Dumper() would write out the string name. Then, I could create the sub ref from the string with \&{$string}. This does work.

The first question is, is this the best way to do it? Or is there another way? For example, given a sub ref is there any way I can convert that back into a string, that could be written by Dumper()? I'm assuming not since if there was, Dumper() would already do it... wouldn't it?

If this is how others would do it, I have some concerns that maybe someone could provide hints about. For example, what if someone fat-fingers the name of the handler? Is there a way in my::Error::register() to "look up" the string and see if it's a real sub, without actually running it, so I can die() immediately in my::Error::register() instead of later when trying to run the error handler?

Second, these plugins are actually separate packages, all inherited from "my::Plugin", so today I can just run register(\&handler) but to use a string I suppose I need my::Error::register('my::Plugin::SomePlugin::handler'). Is there any reasonable way to make this easier? I guess I can always add the 'my::Plugin::' prefix myself, or something, but... I dunno.

Thanks for any hints you can provide!


In reply to Saving/recovering sub refs in a file by madscientist

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.