views:

1100

answers:

2

I'm writing some server code that talks to a client process via STDIN. I'm trying to write a snippet of perl code that asynchronously receives responses from the client's STDOUT. The blocking version of the code might look like this:

sub _read_from_client
{
   my ($file_handle) = @_;
   while (my $line = <$file_handle>) {
      print STDOUT $line;
   }
   return;
}

Importantly, the snippet needs to work in Win32 platform. There are many solutions for *nix platforms that I'm not interested in. I'm using ActivePerl 5.10.

+5  A: 

This thread on Perlmonks suggests you can make a socket nonblocking on Windows in Perl this way:

ioctl($socket, 0x8004667e, 1);

More details and resources in that thread

xdg
http://www.perlmonks.org/?node_id=529812I looked at that link- I was not excited about using 'sysread' and 'ioctl' because they are lower-level than I'd like. If there are other approaches (e.g. a well-accepted CPAN module)- I'd like to know.
schwerwolf
+3  A: 

If you don't want to go the low-level route, you will have to look at the other more frameworked solutions.

You can use a thread to read from the input and have it stuff all data it reads into a Thread::Queue which you then handle in your main thread.

You can look at POE which implements an event based framework, especially POE::Wheel::Run::Win32. Potentially, you can also steal the code from it to implement the nonblocking reads yourself.

You can look at [Coro], which implements a cooperative multitasking system using coroutines. This is mostly similar to threads except that you get userspace threads, not system threads.

You haven't stated how far up you want to go, but your choice is between sysread and a framework, or writing said framework yourself. The easiest route to go is just to use threads or by going through the code of Poe::Wheel::Run::Win32.

Corion