in reply to problem with sleep!
Add $|=1 before the loop, and then learn about $| and output buffering.
% perldoc -q unbuffer [download]
the lowliest monk