如何在Cygwin下运行Perl程序时修复print语句的时序?

时间:2013-08-21 16:51:11

标签: perl cygwin

我写的第一个Perl程序是一个小程序(下面)与贝叶斯定理一起玩。它适用于cmd窗口中的Windows 8以及Linux上,但我最近在Win8机器上安装了Cygwin,并且cygwin中的行为很奇怪。当我运行它时,不会出现打印消息,但是如果我输入三个数字,然后按“输入”,它将响应所有三个打印以及printf。

use strict;
use warnings;
print "What was the previous estimate that the hypothesis is true?\n";
my $x =  <STDIN>;
chomp $x;
$x *= .01;
print "What is the probability of the event if the hypothesis is true?\n";
my $y = <STDIN>;
chomp ($y);
$y *= .01;
print "What is the probability of the event if the hypothesis is false?\n";
my $z = <STDIN>;
chomp ($z);
$z *= .01;
my $bayes = 100 * ($x * $y) / (($x * $y) + $z * (1 - $x));
printf "Posterior probability is %3.2f%%\n", $bayes;

1 个答案:

答案 0 :(得分:4)

这是一个缓冲问题。将stdout设置为unbuffered:

$| = 1

另外,如果你在CMD窗口中运行cygwin,那么cygwin和CMD之间也可能存在一些干扰。尝试在薄薄的窗口中跑步。