in reply to Shell Menu Code Review

One thing that jumps out at me...

There's a lot of really tight connections between your subs: main_block assumes that there are exactly 2 options, user_choice assumes it knows exactly what option 1 and option 2 are, etc.... Subs are really usefull from a design perspective because they setup a "black box" that will take some input, perform some actions, and produce some output. But by interconnecting them so much, you're losing all of their power -- no one can ever modify one of your subs without touching all of the other ones -- which kinda defeats the point.

That's not to say that they way you are doing things is inheriently bad given the scope of your script. I just think you are missleading anybody who ever reads your code in the future. You're script would accutally be more readable if you eliminated the subs, and had a basic sequential series of code.

Replies are listed 'Best First'.
Re: Re: Shell Menu Code Review
by softworkz (Monk) on Jul 25, 2002 at 19:42 UTC
    I think the subs are useful but hossman is right about how you set the subs up
    Here's a sample script that I came up with and maybe you can tinker with it a little so you can have two flavors, this code did work on linux but I only recently tested in windows Xp, hope it helps!
    #!/usr/bin/perl -w use strict; my $choice = ""; # main program while ($choice !~ /q/i) { $choice = &mainmenu(); SWITCH: { $choice =~ /^1/ && do { &option1(); last SWITCH; }; $choice =~ /^2/ && do { &option2(); last SWITCH; }; } } # main menu sub mainmenu { my $input = ""; print "\nUTILITIES\n\n"; print "Please choose from the following options (or Q to quit):\n\ +n"; print "1. Establish PPP Session\n"; print "2. Logoff\n"; print "-------------------------\n"; while () { print "\nYour choice --> "; chomp($input = <STDIN>); if ($input =~ /^\d$/ || $input =~ /^q$/i) { return $input; } else { print "Not a choice. 1-2 or Q to quit, please,\n"; } } } # connect to PPP session sub option1 { print "Connecting to PPP session\n"; } # log off session sub option2 { print "Logging off\n"; }
Re: Re: Shell Menu Code Review
by PrimeLord (Pilgrim) on Jul 25, 2002 at 19:48 UTC
    I see what you mean. I may have to see if I can play with that. I wrote the subs like that so I would have an easy way to regenerate the menu if they chose a wrong option or once the ppp exited, but I guess I could just do that with format too.

    Thanks,
    -Prime