Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Non-blocking read

by kwoff (Friar)
on Nov 12, 2001 at 22:43 UTC ( [id://124878]=note: print w/replies, xml ) Need Help??


in reply to Non-blocking read

Use select. Example in the while loop below:
#!/usr/local/bin/perl -w use strict; BEGIN { # from Camel book 2nd ed. p.474 use POSIX qw(:termios_h); my ($term, $oterm, $echo, $noecho, $fd_stdin); $fd_stdin = fileno(STDIN); $term = POSIX::Termios->new(); $term->getattr($fd_stdin); $oterm = $term->getlflag(); $echo = ECHO | ECHOK | ICANON; $noecho = $oterm & ~$echo; sub cbreak { $term->setlflag($noecho); $term->setcc(VTIME, 1); $term->setattr($fd_stdin, TCSANOW); } sub cooked { $term->setlflag($oterm); $term->setcc(VTIME, 0); $term->setattr($fd_stdin, TCSANOW); } } END { cooked(); } cbreak(); while (1) { my ($rin, $rout, $nfound); $rin = ''; vec($rin, fileno(STDIN), 1) = 1; if ($nfound = select($rout=$rin, undef, undef, 0.001)) { my ($key); sysread(STDIN, $key, 1); if ($key eq 'j') { print "It was j!\n"; } elsif ($key eq 'q') { print "I'm melting!\n"; exit; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124878]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found