in reply to Simple menu..

Do you want people to type simple digits, or strings like 'command1' and 'command2'?

The way it's phrased in your menu, I'd expect to type '1' and have sub1() execute.

Unfortunately, I'd have to type 'command1' to hit the correct hash key.

If my assumption is incorrect, please respond with the data you've entered.

Update: I completely missed this the first time. What you *really* want is:

chomp(my $string = <STDIN> ); if ($commands{$string}) { $commands{$string}->(); } else { print "No such command: $string\n"; }
You were assigning the input to $commands, but not doing anything with it. You also declared $string within the hash check, assuring that it was undefined.

You did get the chomp part correct, which was the problem I had when I first tried something like this, though.

Replies are listed 'Best First'.
RE: RE: Simple menu..
by Anonymous Monk on Oct 11, 2000 at 03:37 UTC
    Sorry for the sloppy post. I want to type in 'command1' and have it run that subroutine.
      You need to dereference the function name, see 11.4 in the cookbook # entering 'happy' runs the routine 'joy' eg %command = ( "happy" => \&joy ); chomp($feeling = <STDIN>); if($command{$feeling}){ $command{$feeling}->(); } sub joy{ print "happy huh \?"; }