markperlb0y has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

So I found out that Win32::Ole and threads don't go well together, and Alarms are difficult to get working on Win32.

I need to time user's input and feed the next instructions with some default values when no input is received in STDIN within a given period. If I call read_excel directly, it will work just fine. If i will call it within a thread, then I will still have to press enter before the script can continue executing. But I need to do it this way, otherwise I'll have another problem with Win32::OLE and threads being not compatible (i.e., the program crashes).

I swear I made this work before but when it was time to clean up my code and re-arrange everything, it stopped working. And I couldn't CTRL+Z anymore in Padre because I already closed the file.

I'm on Windows 7, perl 5.14.2 if that helps.

use 5.014; use threads; use threads::shared; my $input : shared; my $inthread = threads->new (sub { $SIG{KILL} = sub { threads->exit(); }; print "Enter something: "; chomp ($input = <STDIN>); }); sleep 5; if (not defined $input) { $inthread->kill('KILL')->detach; } else { print "we got $input!"; } my $message = 'HELLO FROM SUB!!!\n'; my ($excelthread) = threads->create('read_excel', $message); #read_excel($message); say "\nNEW THREAD SUCCESSFULLY CREATED"; sub read_excel{ say shift; }

Replies are listed 'Best First'.
Re: Timed STDIN input via threads on Win32
by kschwab (Vicar) on Nov 19, 2013 at 02:38 UTC
    See the advice in the second reply in this question.

      That was exactly what I did to solve the thread-safe problem. Inside the read_excel I have that require Win32::OLE line. My problem is that when the timeout expires and I am calling the read_excel sub by creating a thread, then STDIN seems to hang and I have to press enter. If I will call the sub directly, the script continues, however, the problem now becomes the thread-Win32::OLE incompatibility, making the program crash.

        Perhaps you could try Term::Readkey instead? It has built-in support for timeouts. It reportedly works in Win32, and would let you trigger off of a single keystroke, versus the user having to press enter.