Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Tips and Pointers using ncurses with perl

by odie (Sexton)
on Sep 12, 2000 at 22:23 UTC ( [id://32154] : perltutorial . print w/replies, xml ) Need Help??

I just started out with ncurses for the first time. It is hard enough to use the manuals/tutorials written for C. To make it worse, some functions have been renamed or might simply be nonexistant. I have only scratched the surface of the ncurses module, but I have a few pointers, that will save the ones new to CPANs Curses module some time.

1) The functions to read from STDIN are (as usual) blocking. This means, the program will halt on fx. getch() until input is recieved. There are a few ways to make it nonblocking according to the manual. I have found only one that does not cause problems: Use the function halfdelay(ms). The function will wait for ms milliseconds on a getch(), and if no input is recieved, getch() will return "-1".

2) To use color, simply use start_color and define a few color pairs with the function init_pair(pairnr, fg, bg) where fg is the foreground and bg is background. Colors are from 0 to 7. (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White). To use the colors, I have found only two supported functions: attron(ATTR) and attroff(ATTR). "man attr" will show what attributes can be toggled with the functions. To print a line in green, on yellow, you simply use the ATTR COLOR_PAIR(n). Do like this:
init_pair(1,2,3); # Initiate pair. Pair 0 reserved for mono. attron(COLOR_PAIR(2)); # Start color. addstr(15, 30, "MOOOO!");# print string at 30,15. Always use (y,x). attroff(COLOR_PAIR(2)); # End color.

3) The above is also a good example for this point: Some functions have been slightly renamed. attron/attroff is named attr_on/attr_off in C and therefore in all manuals. This can make it difficult to find the correct manpages.

4) I am still looking for a way to hide the cursor. The C man pages have the function curs_set(<mode>). Unfortunately the CPAN Curses seems unable to support this. If anyone finds a way, please let me know. =)

As I said, I barely even scratched ncurses surface, but I hope this will save the ones new to both ncurses and C ( like me ) a few hours of agony.

Replies are listed 'Best First'.
Re: Tips and Pointers using ncurses with perl
by Anonymous Monk on Nov 11, 2002 at 05:22 UTC
    Hi odie!

    curs_set works just fine with CPANs Curses.
    Just call curs_set(0) directly after your initscr(). This will make the cursor go away.

    Michael Wilk

Re: Tips and Pointers using ncurses
by Anonymous Monk on Dec 24, 2002 at 23:35 UTC
    a very straight forward, clear cut introduction to ncurses can be found at: although he does assume you are familiar with a bit of c, he gives you lots of pre-coded examples to hack and learn from, which i believe is the best way to learn. -garbageman
      You should read things before you comment on them.