Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Non-blocking reading on STDIN?

by woosley (Beadle)
on Apr 19, 2009 at 06:49 UTC ( [id://758552]=perlquestion: print w/replies, xml ) Need Help??

woosley has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I want to create a non-blocking read on STDIN, that means I don't have to enter RETRUN, each characters would show up just after I have enter it on the terminal. How can I do this? I used the code below, but it just did not work out?
btw: No Term::ReadKey
#!/usr/bin/perl use strict; use warnings; use Errno qw/EWOULDBLOCK/; use IO::Socket; #my $socket = IO::Socket::INET->new( 'localhost:5555', ) or die "Faile +d"; STDIN->blocking(0); my $data; while (1) { my $rc = sysread( STDIN, $data, 1024 ); if ( defined $rc ) { if ( $rc > 0 ) { # print $socket $data; print $data,"\n"; } else { # close $socket; } } elsif ( $! == EWOULDBLOCK ) { next; } else { die "sysread error:$!"; } } #close $socket;

Replies are listed 'Best First'.
Re: Non-blocking reading on STDIN?
by johngg (Canon) on Apr 19, 2009 at 11:30 UTC

    If you are really unable to use Term::ReadKey then perhaps this node will give you some pointers.

    Cheers,

    JohnGG

Re: Non-blocking reading on STDIN?
by betterworld (Curate) on Apr 19, 2009 at 10:06 UTC
    I want to create a non-blocking read on STDIN, that means I don't have to enter RETRUN, each characters would show up just after I have enter it on the terminal

    I think you mean RETURN. Anyway, the two things you mention are not the same. Blocking mode means that read operations blocks until input is available. Input is available if you have typed either a character or a whole line, depending on the terminal settings. This particular setting is known as "canonical mode". You can test it with the "stty" command, but undoubtedly there CPAN modules to leave and enter canonical mode.

    Another thing one should be aware about: For terminal applications, usually STDIN has the same file descriptor as STDOUT. This means that if you set the non-blocking flag for STDIN, it will be set for STDOUT, too. This means that if you print a long buffer to the terminal, it might be truncated and print returns EAGAIN. But as nobody checks the error code when printing to a terminal (e.g. curses doesn't seem to do so), the terminal will at some point display unexpected contents.

      thanks, betterworld!
      It is related to the setting of the tty. After running command "stty -icanon", it worked out.

      thanks again
Re: Non-blocking reading on STDIN?
by mr_mischief (Monsignor) on Apr 19, 2009 at 07:31 UTC
    "btw: No Term::ReadKey"

    Why?

    Are you on a platform on which it doesn't work? Is there some rule from management that bans using software written elsewhere (in which case, how would one use Perl at all?)? Is Term::ReadKey conflicting with some other part of your project? Does requiring the module put you over some storage limit? Are you unaware that CPAN modules can be installed without a superuser account? Does the test suite fail some criterion of yours?

    It's not quite fair to a community that has provided you a working solution and tons of advice on how to use the publicly available tools on CPAN to just dismiss a module out of hand without giving a reason. If you have a reason not to use a CPAN module, please have the respect for those reading the thread to state it. That way we can avoid guessing at your issue and get right to either solving it or working around it.

      I have no problem with Term::ReadKey...
      I was just tring to understand how non-blocking IO working by doing this. Term::ReadKey would solve my "Read without RETURN" problem, I knew that, but it dose not have anything to do with non-blocking IO. SO......

      Maybe I have used the wrong words?
      Sorry for my poor English...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found