Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!rutgers!mcnc!uvaarpa!mmdf From: marc@athena.mit.edu Newsgroups: comp.lang.perl Subject: Re: Wanna help us write The Book? Message-ID: <1990Jul26.234220.28347@uvaarpa.Virginia.EDU> Date: 26 Jul 90 23:42:20 GMT Sender: mmdf@uvaarpa.Virginia.EDU (Uvaarpa Mail System) Reply-To: marc@mit.edu Organization: The Internet Lines: 90 Will it be possible to see the book online before it is too late to make changes? I find it much easier to provide context diffs than nebulous suggestions. Also, here's a package I wrote which makes interactive perl scripts easier to write. Given an array of the form: @menu = ( "command1","perlsub1","help", "command2","perlsub2","help2", ); It goes into a loop prompting for commands and executing them. "exit" exits the loop, "quit" or ^D exits the program, and "help" displays help. --cut-- # package, meant to be do'ed, not run directly # # $Id: menu.pl,v 1.1 90/07/25 02:57:15 marc Exp $ # package menu; @gencmds = ( "help","__internal","Print the list of commands in this menu", "?","","help", "exit","__internal","Exit the current menu", "quit","__internal","Quit", ); sub main'menu { # @_ = ($prompt,@cmds) local($prompt,@cmds,%fcts,%helps); $prompt = shift(@_); @table = (@_,@gencmds); while (@table > $[) { $cmd = shift(@table); $fct = shift(@table); $help = shift(@table); if (defined($fcts{$cmd})) {next;} if ($fct eq "") { $fct = $fcts{$help}; $help = $helps{$help}; } push(@cmds,$cmd); $fcts{$cmd} = $fct; $helps{$cmd} = $help; } while (1) { print "\n$prompt: "; if (!($_ = <>)) { print "eof on input. Aborting...\n"; exit(2); } chop; if (defined($fct = $fcts{$_})) { if ($fct eq "__internal") { # Magic... if (($_ eq "help") || ($_ eq "?")) { foreach (@cmds) { printf "%-15s%s\n",$_,$helps{$_}; } } elsif ($_ eq "exit") { return; } elsif ($_ eq "quit") { exit(0); } else { die "Bogon __internal $_!\n"; } } else { if (index($fct,"'") == ($[-1)) { $fct = "main'".$fct; } eval("&$fct();"); } } else { print "\"$_\" is not a valid command. Type ? for help.\n"; } } } --cut-- One thing I've thought about adding is argument parsing. Maybe even an eval statement, so you can answer a prompt with perl code :-) Other ideas? Marc