MainLoop: while ($packet = getpacket()) { if ($packet == RESPONSE_PACKET) { deal_with_response(); } else { dispatch_packet(); } } sub dispatch_packet { my $packet = shift; if ($packet == NORMAL_PACKET) { ordinary_handler(); } elsif ($packet == SPECIAL_PACKET) { special_handler(); } } sub ordinary_handler { whatever(); } sub special_handler { ask_for_info(); # save the state so that we can deal # with the response later save_state(); } sub deal_with_response { # get the saved state and deal with the response load_state(); talk_to_the_hand(); } __END__