我编写的第一个 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;