in reply to Re^3: Any event modules that support STDIN polling for Windows?
in thread Any event modules that support STDIN polling for Windows?

HOLY COW! I'm totally inspired by your "nothing cannot be done in Perl" attitude. It took me some time to digest that code, and I finally came up with my interpretation of that. I hope this helps many others who are on the same (average) level as me and facing the same problem too!
#!/usr/bin/perl use strict; use warnings; use Carp; use English '-no_match_vars'; use charnames ':full'; use Readonly; use Readonly::XS; use threads; use IO::Socket; use version; Readonly::Scalar our $VERSION => qv(0.0_1); $OUTPUT_AUTOFLUSH = 1; # IOCTL code to enable or disable non-blocking mode on a socket Readonly::Scalar my $FIONBIO => 0x80_046_67e; # Create a UDP server socket as input??? Pick a random port number my $in_sock = IO::Socket::INET->new( LocalAddr => 'localhost', LocalPort => 0, Proto => 'udp' ) or croak 'Failed to open port'; # Enable the newly created input socket's non-blocking mode my $true = 1; ( ioctl $in_sock, $FIONBIO, \$true ) or croak $ERRNO; # Create a UDP client socket as output??? Bind it to the input socket +created # earlier my $out_sock = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => $in_sock->sockport(), Proto => 'udp' ) or croak "Failed to bind remote port $in_sock->sockport()"; # Don't really get the mechanics behind these. Just copy and apply for + now threads::async { while (<>) { $out_sock->send($_); } } ->threads::detach; # A simple test to prove that it is non-blocking while (1) { sleep 1; print "\N{FULL STOP}"; } 1;
While the script is running, I can even hit Backspace to erase the contents on the screen!

Replies are listed 'Best First'.
Re^5: Any event modules that support STDIN polling for Windows?
by BrowserUk (Patriarch) on Aug 30, 2010 at 18:51 UTC
    # Don't really get the mechanics behind these. Just copy and apply for now.

    There's nothing complicated there. It simple detaches a thread that does nothing but read lines (blocking) from STDIN, and when it gets one, echoes it to the write end of a socket pair. The read end of that socket pair is set non-blocking, so now it can be used in select loops in the normal way.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: Any event modules that support STDIN polling for Windows?
by BrowserUk (Patriarch) on Sep 08, 2010 at 09:36 UTC
    It took me some time to digest that code, and I finally came up with my interpretation of that. I hope this helps many others who are on the same (average) level as me

    I remember meaning to comment on this at the time, but then forgetting to.

    Why you think your implementation is any clearer than mine?

    For a start, prefixing method names with their package names is down right bizarre:

    threads::async { ... } ->threads::detach;

    Even though it appears to work in this case, it surely won't in most.

    And why turn one simple line $out->send( $_ ) while <STDIN>; into into 3?

    I'm totally inspired by your "nothing cannot be done in Perl" attitude.

    Hm. Whilst it may be true that "nothing cannot be done in Perl", there are many things that I wouldn't even attempt in Perl.

    More applicable is that anything that can be done using events, can be more simply and without "turning your program inside out", using threads.


    And finally, I somehow doubt that you are quite as "average" as you make out. Using LocalPort => 0 shows knowledge of IO::Socket::INET that isn't even documented.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.