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

Hello!

I would like to ask on how to make my subroutines or how can my subroutines can be used for menu selection type of script?

What I want to achieve is:

I have two sub routines, if I choose the 1st sub routine it will process it and after processing will go back to the menu selection again (same with the 2nd sub routine if chosen it will go back to the main menu)if i choose the 3rd selection then the program will stop or exit.

hope you can help me still I'm still learning how sub routines work. :) thanks

  • Comment on How to make a menu using my subroutines?

Replies are listed 'Best First'.
Re: How to make a menu using my subroutines?
by NetWallah (Canon) on Apr 03, 2012 at 04:36 UTC
    Subroutines are written like this:
    sub Subroutine_1_name{ #Some code here }
    Menus can be printed using the "print" statement.

    . Input from STDIN can be read using the "<>" construct.

    Choices between alternatives can be made using the "if" statement.

    Loops can be made using the "while" statement.

    Please research these components - they are sufficient to construct your program.

    If you show us you tried something, and it did not work, we would be happy to help you.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

      I tried something but the problem is it won't go back to the main menu when I picked 1 or 2.

      Here's my test code for checking ping and DNS of a host (just a part of it for the menu testing).

      use Net::Ping; require LWP::UserAgent; use HTTP::Headers; use NET::DNS; use Socket; use NET::IP; use Time::HiRes qw( gettimeofday ); my @url = ('www.google.com', 'www.yahoo.com'); sub exit_st(); sub check_ping(); sub check_dns(); open(OUTFILE, ">", "check_result3.txt") ? print "\n\nProcessing the re +sult...\n\n" : die "Can't process the result, unable to write output: + $!"; %action = ( '1' => \&check_ping, '2' => \&check_dns, '3' => \&exit_st,); print <<"EOT"; Select one of: 1. Ping Check 2. DNS Check 3. Exit EOT print "Enter your choice here: "; my $menu_item = <>; chomp($menu_item); (defined $action{$menu_item}) ? $action{$menu_item}->() : print "Wrong + input. \n"; exit 0; #Check Ping sub check_ping() { print OUTFILE "*Ping Result*\n"; my $p = Net::Ping->new("icmp"); ($p->ping($url[0])) ? print "$url[0] is alive.\n" : print "$url[0] is +not alive\n"; ($p->ping($url[1])) ? print "$url[1] is alive.\n\n" : print "$url[1] i +s not alive.\n\n"; return 0; } #Check DNS sub check_dns() { print OUTFILE "*DNS Result*\n"; $ip1 = gethostbyname($url[0]) || die "error - gethostbyname: $!\n\n"; $hostip1 = inet_ntoa($ip1) || die "error - inet_ntoa: $!\n\n"; print $url[0], " = ", $hostip1; return; }
      I don't know where the looping should take place, so confused hehe.. thanks

        You want something like:

        my $menu_item; while (lc $menu_item ne 'q') { # allow 'q' to quit print "Enter your choice here: "; $menu_item = <>; chomp($menu_item); (defined $action{$menu_item}) ? $action{$menu_item}->() : print "Wro +ng input. \n"; }
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'