Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Automate Making Menus

by staeryatz (Monk)
on Oct 29, 2001 at 23:08 UTC ( #121962=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl -w
   2: 
   3: # this is just a couple of subroutines
   4: # that automate the creation of menus
   5: # for CHUI programs.
   6: #
   7: # it will print a neat looking menu,
   8: # chomp the user input and return it.
   9: #
  10: # it's very simple and really nothing
  11: # to show off, but definately something
  12: # to share. :)
  13: 
  14: 
  15: sub ask($) {          # takes a question as a parameter
  16: 
  17:  print $_[0];
  18:  my $INput = <STDIN>;
  19:  chomp $INput;
  20:  return $INput;       # and returns the user input
  21: } # end ask() -----------------------------------------
  22: 
  23: 
  24: sub mkmenu(@) {       # takes an array of menu options
  25: 
  26:  my $title = shift @_;    # first cell is the title
  27:  my $question = pop @_;   # last cell is the question
  28: 
  29:  print "\n" x 25, "=" x 80;
  30:  print " " x (40 - length($title) / 2);
  31:  print "$title\n", "=" x 80, "\n\n";
  32: 
  33:  my $i = 1;
  34:  foreach $el (@_) {
  35:   print "\t\t\t$i\t$el\n";
  36:   $i++;
  37:  }
  38:  print "\n", "=" x 80;
  39:  $i = 1;
  40:  foreach $el (@_) {
  41:   print " $i=$el |";
  42:   $i++;
  43:  }
  44: 
  45:  ask($question);
  46: } # end mkmenu() --------------------------------------
  47: 
  48: 
  49: # the following is a sample of how to use it
  50: 
  51: my @menuItems = ("MainMenu",
  52:                  "Input File", "Output file", "Help", "Options", "Exit",
  53:                  " Choose: ");
  54: 
  55: my $choice = mkmenu(@menuItems);
  56: print "Your choice is $choice\n";
  57: 
  58: 
  59: # the following is the result of the above code
  60: #
  61: #================================================================================
  62: #                                    MainMenu
  63: #================================================================================
  64: #
  65: #			1	Input File
  66: #			2	Output file
  67: #			3	Help
  68: #			4	Options
  69: #			5	Exit
  70: #
  71: #================================================================================
  72: # 1=Input File | 2=Output file | 3=Help | 4=Options | 5=Exit | Choose:
  73: 
  74: 

Replies are listed 'Best First'.
Re: Automate Making Menus
by premchai21 (Curate) on Oct 29, 2001 at 23:54 UTC
    Not bad; have you considered using Term::ReadKey for single-character input and perhaps Curses for terminal control?
      Thanks.

      Currently, I don't know how to use modules, I'm new to Perl. It also seems that much of the things I write have already been done, usually alot more comprehensive than my own.

      Nobody likes to reinvent the wheel, unless they can make it rounder. My above script is just a smaller, simpler wheel.

Re: Automate Making Menus
by jeroenes (Priest) on Oct 30, 2001 at 02:48 UTC
    CPAN is your friend and YAPPC, of course

    There already is a perlmenu package thingy. I've used it for a couple of tty scripts, and it worked reasonably well. Easy to set up menus, and has all the features you could wish for: control chars for 'control', radio buttons, hierarchical menus, form fields, multi-page menus, etc etc.

    It's ncurses based, and I checked the homepage, it can even be found on CPAN. Not updated since 1997, it seems, but it works. Check it out at perlmenu. It's quite easy to use.

    To support the last statement, I could even use it before joining PM. That says something {grin}.

    Cheers,

    Jeroen
    "We are not alone"(FZ)

      Thanks.

      I downloaded it. Now it's just a matter of figuring out how to use it.

      Ncurses is pretty cool. Currently, I code in Unix shell + dialog
      if I want to make ncurses menus. Here's a look at one of them.

      Something I've used and abused in the above code is Perl's
      string multiplier 'x'. It comes in handy:
      print "=" x 80;

      Some languages don't have something so cool like that.
        Hanging around the monastry will learn you many more neat tricks. To name a few:
        1. use strict and warnigns and diagnostics or die
        2. lingual-like for/while/if/unless constructs, eg.
          print "I love perl ".$_++." times\n" while m/vroom/; print "Bwech" unless m/perl/ or not substr($^O, 'linux' );
        3. split (/tell chipmunk I did resist the temptation)
        4. eval
        5. .... and many others.
        Mehopes you will enjoy the wonders of perl!

        Jeroen
        "We are not alone"(FZ)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlcraft [id://121962]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-05-18 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (70 votes). Check out past polls.

    Notices?