in reply to Re^2: Any event modules that support STDIN polling for Windows?
in thread Any event modules that support STDIN polling for Windows?
I can improve its responsiveness to 0.2 seconds maybe
There is nothing to stop you calling it every 0.001 seconds if you think the typist is likely to enter something significant within that time. Of course, the problem with this kind of architecture is that you have to do you own line buffering, getline-style editing, history etc.
However, here's a more convenient alternative. It uses a thread to send any input from STDIN to a selectable socket. With the advantages that a) you don't need to set up callbacks or artificial timers; b) all the standard command line handling is available to the user; c) line buffering and re-assembly is taken care of:
#! perl -slw use strict; use threads; use IO::Socket; use IO::Select; $|++; sub selectableSTDIN { my $true = 1; my $in = IO::Socket::INET->new( LocalAddr => '127.0.0.1:65521', Proto => "udp", ) or die "Failed to bind port 65521"; ioctl( $in, 0x8004667e, \$true ) or die $!; my $out = IO::Socket::INET->new( PeerAddr => '127.0.0.1:65521', Proto => "udp" ) or die "Failed to bind remote port 65521"; async { $out->send( $_ ) while <STDIN>; }->detach; return $in; } my $sel = IO::Select->new( selectableSTDIN() ); while( 1 ) { for my $src ( $sel->can_read( 0.1 ) ) { print "\n", scalar <$src>; } printf '.'; } __END__ C:\test>selectableSTDIN.pl ...........f.r..e.d.. fred ........b..i....l.l... bill ..........j..o..h..n... . john .......j.a...c...k... jack ........Terminating on signal SIGINT(2)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Any event modules that support STDIN polling for Windows?
by KHLeow (Novice) on Aug 30, 2010 at 18:37 UTC | |
by BrowserUk (Patriarch) on Aug 30, 2010 at 18:51 UTC | |
by BrowserUk (Patriarch) on Sep 08, 2010 at 09:36 UTC |