4
votes

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();
1
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
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

1 Answers

2
votes

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).