RazorbladeBidet means the loop may in fact be ending without you realizing it because of buffering. If
select(STDOUT); $|=1; helps, it means the problem is actually after the loop, and that you got fooled into thinking it was within the loop because the printed data was still in a buffer.