You might want to look at
Event, which turns the problem sideways,
and in some ways, makes it easier to expand and design. Your main loop
can either run the event loop at known steps, or you can have the event-loop
call a piece of your main loop when it's otherwise idle.
-- Randal L. Schwartz, Perl hacker