in reply to How to make a menu using my subroutines?

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)

Replies are listed 'Best First'.
Re^2: How to make a menu using my subroutines?
by astronogun (Sexton) on Apr 03, 2012 at 06:02 UTC

    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'
        yes like that, but much better if the selection or the menu will also appear again and then you will choose again the number. Edited: Already figured it out :) thanks for the help tobyink