Perl simple text menu with options

Hopefully I'm in the right place. Im new to the forums and linux!

I'm looking to add a menu to my perl hangman game i have created. The menu will use user input for the desired option and then perform the operation indicated. I would like something along the lines of:

Welcome to Hangman
If you would like to continue press 1 (calls module to run hangman game)
Press 0 to exit (exits the script)

prior to writing the hangman script i did not declare which version to use so im assuming its running an older version.

Similarly i need another menu that has 3 options that open up to another menu.

For instance:

  1. system commands
  2. edit commands > opens menu with 3 options that just print something
  3. admin commands > opens menu with 3 options that each print something
  4. network commands > opens menu wtih 3 options that each print something
  5. games
  6. exit

some sort of input validation would be nice to.

Any and all help would be greatly appreciated. thank you.

The following code demonstrates a basic menu by using <STDIN> for reading input.

#!/usr/bin/perl

use strict;
use warnings;
use Switch;

my $input = '';

while ($input ne '6')
{
    clear_screen();

    print "1. system commands\n".
          "2. edit commands\n". 
          "3. admin commands\n". 
          "4. network commands\n". 
          "5. games\n".
          "6. exit\n";

    print "Enter your choice: ";
    $input = <STDIN>;
    chomp($input);

    switch ($input)
    {
        case '2'
        {
            $input = '';

            while ($input ne '4')
            {
                clear_screen();

                print "1. edit > option 1\n".
                      "2. edit > option 1\n".
                      "3. edit > option 3\n".
                      "4. return to main menu\n";

                print "Enter your choice: ";
                $input = <STDIN>;
                chomp($input);
            }

            $input = '';
        }

    }
}

exit(0);

sub clear_screen
{
    system("clear");
}
1 Like