I too have had issues with alarm() on Windows. This is a quick workaround - not very efficient or accurate, but close to the timeout you want. $timeout is number of seconds to run before timing out.
NOTE: I need an "infinite output" .exe so the './test.exe' program just prints numbers 1 .. 100000 to the screen. I set my $timeout for 2 seconds.
use strict;
use warnings;
my $pid = open(my $fileHandler, '-|', "./test.exe" );
my $start = time;
my $timeout = 2;
while (<$fileHandler>) {
print "$_\r"; # \r to not overrun output screen buffer
last if ((time - $start) >= $timeout);
}
print "\nTimeout! - Time to kill pid: $pid\n"
And the output:
VinsWorldcom@C:\Users\VinsWorldcom\tmp> TimeThis test.pl
TimeThis : Command Line : test.pl
TimeThis : Start Time : Fri Oct 12 10:42:04 2012
56464
Timeout! - Time to kill pid: 4192
TimeThis : Command Line : test.pl
TimeThis : Start Time : Fri Oct 12 10:42:04 2012
TimeThis : End Time : Fri Oct 12 10:42:06 2012
TimeThis : Elapsed Time : 00:00:01.903
Of course you could maybe use Time::HiRes to get more accurate.