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

I am trying to get ncurses to allow me to read nonblocking from the keyboard. It seems like it is impossible to have getstr() read nonblocking, so I tried using getch(). I found two ways of making a nonblocking read:

timeout(1) When using this, getch() returns "\n" if there is no data from keyboard.

nodelay(stdscr) When using this, getch() becomes blocking.

So there is the question. How do I use readch() nonblocking and keep the return key functioning? Or, how can I make a nonblocking readstr()?

The documentation for ncurses is all for C, and I find little documentation to speak of for perl ncurses.

Replies are listed 'Best First'.
Re: nonblocking read with ncurses
by BastardOperator (Monk) on Sep 09, 2000 at 05:43 UTC
    I have no idea about curses, but would it maybe be possible to stop the timeout (or set it real high) and do something like:
    while(1) { my $chr = getch(); if($chr ne "\n") { push(@someary, $chr); } else { print "\n" last; } }
    I'm not sure if this would help you at all, and I haven't tested that code, so take it with a grain of salt.
RE: nonblocking read with ncurses
by odie (Sexton) on Sep 10, 2000 at 17:31 UTC
    Ok. Found it: trick is to use "halfdelay(1);". problem solved.
RE: nonblocking read with ncurses
by odie (Sexton) on Sep 10, 2000 at 17:34 UTC
    Ok. Found it: trick is to use "halfdelay(1);". problem solved.
Re: nonblocking read with ncurses
by AgentM (Curate) on Sep 09, 2000 at 20:58 UTC
    Better solution: threads. Pthreads or PerlThreads reeally doesn't make a difference. But you could kill a thread a number of seconds after waiting on blocking I/O. Threads pose a great solution for "non-blocking " blocking I/O. Hit perlthrtut.