in reply to undef Package;

Take a look at the mod_perl Apache::Run package. It's intended for a similar purpose (to let possibly un-mod_perl friendly CGIs run without alteration).

You might also could use a pool of seperate helper processes. Each helper handles a particular chunk (or chunks) of your dirty code. You hand requests off to these helper processes (through pipes, or over a socket, or getting fancy XML-RPC), the helpers run the dirty code and collect its return results, then send you the results back. If you need a helper for a new chunk of code, kill off the least recently used existing helper and spawn off a new helper for that chunk.