in reply to Win32 console polling

On Windows, select is only supported for sockets (it's not perl fault but a limitation of the crappy OS).

Some days ago, to solve a similar problem (Windows, pipes and multiplexing IO), I created Win32::Socketpair, a module that emulates socketpair with TCP socket connections going through the localhost interface. For instance:

use Win32::Socketpair 'winopen2'; my ($pid, $socket) = winopen2(@cmd) or die "unable to run command @cmd"; my $fno = fileno $socket; my $v = ''; vec($v, $fno, 1) = 1; while(1) { my $vin = $v; my $vout = length $data ? $v : ''; if (select($vin, $vout, undef, undef)>0) { if (vec($vin, $fno, 1)) { sysread($socket, my $buffer, 2000) or last # closed } if (vec($vout, $fno, 1)) { syswrite($socket, $data) or last # closed } } }

You can also use pipes created this way with IO::Select.

I believe that setting the socket to non-blocking is not needed if you are going to use it exclusively inside a select loop, but anyway, on Windows to set non-blocking mode for a socket you have to use this code:

ioctl($socket, 0x8004667e, 1);
Non-blocking socket read on Windows contains more information on the matter and links to other related nodes.

Replies are listed 'Best First'.
Re^2: Win32 console polling
by TheDauthi (Sexton) on Mar 29, 2006 at 14:22 UTC
    Thanks.

    I'm going to take a look at that module and see if it works for what I need. Using loopback and sockets had not occurred to me.

    It's not the socket blocking I'm worried about, but the PIPE I was using from open2. I guessed that the problem was that select in windows only works on sockets, but you can supposedly use WaitForMultipleObjectEx and some of the other winsock functionality to create a select that actually works on any signalable object. I've even been considering writing a module to that effect in C.