Search code examples
perlcygwin

How to fix timing of print statements when running Perl program under Cygwin?


The first Perl program I wrote was a little routine (below) to play with Bayes's Theorem. It works on Windows 8 in the cmd window and also on Linux but I recently installed Cygwin on the Win8 machine and the behavior in cygwin is odd. When I run it no print message appears but if I enter three numbers each followed by "enter" it responds with all three prints as well as the 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;

Solution

  • This is a buffering problem. Set stdout to unbuffered:

    $| = 1
    

    Also, if you're running cygwin in a CMD window there may be some interaction between cygwin and CMD interfering as well. Try running in a mintty window.