perl程序的奇怪行为

时间:2014-03-31 10:42:34

标签: perl sleep

我写了以下程序

if(open(DATA , ">dddddddd")){
 print "creating dddddddd";
 close(DATA);
 }
 else{
    print "cannot create";
 }
 print "going to sleep";
 sleep (10);
 print "unlinking dddddddd";
 unlink "dddddddd";

现在而不是将输出作为

creating dddddddd  
going to sleep  
and after 10 seconds printing
unlinking dddddddd   

程序执行时会休眠10秒,输出如下

creating dddddddd  
going to sleep  
unlinking dddddddd.

任何人都可以解释????

2 个答案:

答案 0 :(得分:4)

您可能希望在脚本的开头设置STDOUT to autoflush

$| = 1;

use IO::Handle;
STDOUT->autoflush(1);

答案 1 :(得分:0)

我们的打印字符串后没有回车符,因此输出可能是缓冲的。你应该添加回车。

例如:

print "creating dddddddd\r\n";

或者只是:

print "creating dddddddd\n";