in reply to behaviour of STDOUT and STDERR

You're Suffering from buffering. Put this before print():
$| = 1; # disable buffering STDOUT