madscientist has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Saving/recovering sub refs in a file
by ikegami (Patriarch) on Jun 16, 2010 at 00:28 UTC | |
by madscientist (Novice) on Jun 16, 2010 at 15:24 UTC | |
by ikegami (Patriarch) on Jun 16, 2010 at 18:27 UTC | |
by madscientist (Novice) on Jun 16, 2010 at 19:42 UTC | |
by ikegami (Patriarch) on Jun 16, 2010 at 20:38 UTC | |
| |
|
Re: Saving/recovering sub refs in a file
by cdarke (Prior) on Jun 16, 2010 at 13:18 UTC | |
by madscientist (Novice) on Jun 16, 2010 at 15:29 UTC | |
by Khen1950fx (Canon) on Jun 16, 2010 at 23:18 UTC | |
|
Re: Saving/recovering sub refs in a file
by repellent (Priest) on Jun 16, 2010 at 22:13 UTC |