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

I am trying to use Curses::UI to provide five menus whose contents should be read from text files. The tutorial offers this example of creating a menu:

my @menu = ( { -label => 'File', -submenu => [ { -label => 'Exit ^Q', -value => \&exit_dialog }, { -label => 'Wait ^W', -value => \&remain_dialog } ] }, }

I need to read each -label from a line of the text file and pass the name of the menu (== the filename) and the label to the subroutine called when the item is selected.

I can see that I need to manipulate some hashes but not how to do it.

Replies are listed 'Best First'.
Re: trying to populate curses menus from text files
by kcott (Archbishop) on Nov 01, 2013 at 04:29 UTC

    G'day boscaiolo,

    Welcome to the monastery.

    Unfortunately, as already pointed out, your post is lacking various pieces of information that would help us to help you. In addition to a lack of details about the input data, there's no concrete indication of your expected output or what, if anything, you've already tried and perhaps had difficulties with. Furthermore, you've provided no links to information you've made reference to: I could probably work out where Curses::UI is documented but I shouldn't have to; I've got absolutely no idea what "tutorial" you're referring to — please read "What shortcuts can I use for linking to other information?".

    From the information you have provided, I suspect your code would look something like this:

    my @list_of_files = qw{File Edit Help}; my $regex_to_match_label = qr{LABEL="([^"]+)}; my @menu; for my $filename (@list_of_files) { open my $fh, '<', $filename or die "Can't read $filename: $!"; my @submenu; push @menu, { -label => $filename, -submenu => \@submenu }; while (<$fh>) { if (/$regex_to_match_label/) { my $label = $1; push @submenu, { -label => $label, -value => sub { generic_dialog($la +bel) } }; } } close $fh; }

    -- Ken

      Ken:

      Thanks for your reply. You divined my intention exactly in spite of my rather minimal information! In fact, I do not need the regex match since the text file comprises one item per line (as I omitted to explain), so my $label = $_; is all I need.

      Your suggested is lucid and perfect to my needs. I don't know why I find hash and array operations so confusing.

Re: trying to populate curses menus from text files (text format)
by Anonymous Monk on Nov 01, 2013 at 02:45 UTC

    ... should be read from text file ...

    What text file where? :) How do I post a question effectively?

    my @menu = ( { -label => 'File', -submenu => [ { -label => 'Exit ^Q', -value => '\&exit_dialog' }, { -label => 'Wait ^W', -value => '\&remain_dialog' } +, ], }, ); use JSON; print encode_json({menu=>\@menu}); __END__ { "menu" : [ { "-label" : "File", "-submenu" : [ { "-label" : "Exit ^Q", "-value" : "\\&exit_dialog" }, { "-label" : "Wait ^W", "-value" : "\\&remain_dialog" } ] } ] }

    Then

    my $menu = decode_json( path( $jsonfile )->slurp_raw ); my $funcname = $menu->{menu}[0]{"-submenu"}[0]{"-value"}; my $funcref = $callback{$funcname}; $menu->{menu}[0]{"-submenu"}[0]{"-value"} = $funcref;

    I need to read each -label from a line of the text file and pass the name of the menu (== the filename) and the label to the subroutine called when the item is selected.

    Your callback will get the menuitem object as argument, you can (usually) query/interrogate/examine this to retrieve the label

      Thanks for your rapid response, and apologies for having over-pruned my question. To my good fortune Ken (below) provided an answer even closer to my intentions.