Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: 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.

In reply to Tips and Pointers using ncurses with perl by odie

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2023-12-05 22:29 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (29 votes). Check out past polls.