in reply to problem with input command

Using a perl type case statment (sometimes called a hash) can work well for this type of problem.
sub help { print(@_); print(" This is help?\n"); } sub list { print("This is list\n"); } our $commands = { 'help' => \&help, 'list' => \&list, }; while (<STDIN>) { chomp; if ((my $name) = ($_ =~ /^(\w+)/)) { if (defined $commands->{$name}) { $commands->{$name}->($_); } else { print "I Don't understand $name\n"; } } }
-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re^2: problem with input command
by muba (Priest) on Mar 27, 2005 at 12:35 UTC

      As it was posted only 2 minutes after yours, there's a good chance that it was submitted without gam3 having seen your post. (I know I have tendency to do more than one thing at once, so the posting time of my messages may have no direct relation to when I read the thread, especially for longer posts, or those that contain code samples that I want to verify beforehand).

      I'm guessing that if there was a specific reason that someone would post a varient of someone else's code, they would make reference to it, and explain why they've decided to take the alternative approach.