4

From the ncurses(3) linux man page:

The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed.

Why doesn't in my example getch wait until I press a key?


#!/usr/bin/env perl6
use v6;
use NativeCall;

constant LIB = 'libncursesw.so.5';
constant ERR = -1;
class WINDOW is repr('CPointer') { }

sub initscr()                         returns WINDOW is native(LIB) {*};
sub cbreak()                          returns int32  is native(LIB) {*};
sub nodelay(WINDOW, bool)             returns int32  is native(LIB) {*};
sub getch()                           returns int32  is native(LIB) {*};
sub mvaddstr(int32,int32,str)         returns int32  is native(LIB) {*};
sub nc_refresh() is symbol('refresh') returns int32  is native(LIB) {*};
sub endwin()                          returns int32  is native(LIB) {*};

my $win = initscr();  # added "()"
cbreak();
nodelay( $win, False );

my $c = 0;
loop {
    my $key = getch(); # getch() doesn't wait
    $c++;
    mvaddstr( 2, 0, "$c" );
    nc_refresh();
    next if $key == ERR;
    last if $key.chr eq 'q';
}

endwin();
brian d foy
  • 129,424
  • 31
  • 207
  • 592
sid_com
  • 24,137
  • 26
  • 96
  • 187
  • This seems to work for me with ncurses 6, and perl6 --version of "This is Rakudo version 2016.02 built on MoarVM version 2016.02 implementing Perl 6.c." What version of Perl 6 are you running? – Coke Mar 21 '16 at 12:32
  • Coke: perl6 -v: "This is Rakudo version 2016.02-151-gb243a96 built on MoarVM version 2016.02-33-g1e3d2ac implementing Perl 6.c." and ncurses 5. – sid_com Mar 21 '16 at 17:26

1 Answers1

2

The equivalent in C works - something odd with your configuration. I don't have a perl6 setup to debug it with, in any case.

The only odd thing I see in the program is that you omitted the "()" after initscr, which I would expect to see for consistency. In C, if you did that, the subsequent calls would dump core (since &initscr is a valid pointer).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • I added the `"()"` to `initscr` but `getch` still doesn't wait. – sid_com Mar 21 '16 at 05:40
  • It's valid Perl 6 syntax to omit the parens. From the docs: "Arguments are supplied as a comma separated list. To disambugiate nested calls parentheses or adverbial form can be used." https://doc.perl6.org/language/functions#Arguments – donaldh Mar 24 '16 at 12:24