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

Dear Monks

I have developed a small script based heavily on the demo script 'demo.menu' which comes with the Curses 1.06 module. However, as soon as I try to build any menus dynamically from an array I get weird results. The code below demonstrates the problem :-

#!/usr/local/bin/perl use strict; use warnings; use Curses; my @test_array; push @test_array,"item 1|description 1"; push @test_array,"item 2|description 2"; my $count=0; foreach $record (@test_array) { split /\|/,$record; $item=new_item($_[0],$_[1]); push @il,$item; push @pack,${$item}; };
However the menu which appears looks something like :-
---------------------------
| ->item 2 description 2  |
|   item 2 description 2  |
---------------------------
i.e. shows the 2nd (or last) item in the array twice. Can anybody explain why, or offer a better example script which makes use of the Curses module.

Many thanks

Myomancer

Replies are listed 'Best First'.
Re: Curses curiosity
by Roger (Parson) on Nov 06, 2003 at 14:58 UTC
    You need to read the doco for curses(3) more carefully -

    The function new_item allocates a new item and initializes it from the name and description pointers. Please notice that the item stores only the pointers to the name and description. Those pointers must be valid during the lifetime of the item. So you should be very carefull with names or descriptions allocated on the stack of some routines.

    This explains why you are only getting the last item, item 2, in your menu. Afterall, perl Curses module is just a wrapper for the Curses library.

Re: Curses curiosity
by myomancer (Novice) on Nov 06, 2003 at 15:00 UTC
    Please ignore this question. It is fatuous. More to the point, a colleague has kindly explained the error (new_item is returning the same reference every time it is called). This would have been obvious if I had any documentation about Curses. Pray tell, what is the best Perl Curses resource out there?

    Humbly

    Myomancer
      You can have a look at the Curses::UI module on CPAN. It has most of the hard work already done for you.