Someone already pointed out that you need to define your dispatch table a little differently. Your example is creating a hash of scalar references, instead of a hash of subrefs. I won't belabor that point since it's already been made.
I did want to mention that your dispatch table needn't use named subs at all. Consider the following code:
my %dispatch = ( DoThis => sub { print "You asked me to do this.\n"; }, DoThat => sub { print "You asked me to do that.\n"; } );
For trivial subs, defining them inline like this can be a simple, convenient, and readable solution.
Another solution is the switch construct:
SWITCH: for ( @commands ) { m/^This/ && do { print "You asked me to do this.\n"; next SWITCH; }; m/^That/ && do { print "You asked me to do that.\n"; next SWITCH; }; m/^Another/ && Another_Way(); m/^Quit/ && do { print "Goodbye.\n"; last SWITCH; } sub Another_Way { print "You asked me to do it another way.\n"; }
Of course there are other ways to do it as well... This is Perl after all.
Dave
In reply to Re: menu script
by davido
in thread menu script
by yoda54
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |