mod_perl does what you want by loading your code into an anonymous coderef and calling it when needed. You could do the same by simply turning the script into an infinite loop with a sleep or possibly some kind of polling, if external input is required