i am designing a CLI. once a user starts the script he has to enter a command.based on that command the script performs the required action.
here is my code
$myhash{"set time"}=\&settime;
$myhash{"show time"}=\&showtime;
$myhash{default}=\&error;
print "Welcome to nMetrics application monitor\n";
print "Please type in a command\n";
while(<STDIN>)
{
chomp;
$res="false";
foreach $k (keys %myhash)
{
if ( $k eq "$_" )
{
&{$myhash{$k}}();
$res="true";
}
}
if ($res eq "false")
{
&{$myhash{default}}();
}
}
so basically i put all the commands in a hash and read from stdin and than invoke the required command.
but later on these commands would increase to more than 20. and having such a big hash would not be a good idea.
so is there any more efficient way of handling this problem?
previously i was doing it using if else statements. but thanks to one of the monks who gave me this wonderful idea.
is there a more smarter idea than this?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.