The worst thing about long if-elsif-else chains is their performance, but you are talking about an interactive aplication, so you don't need to worry about performance and chained if-elsif-else's will not be so bad.
Anyway, let my suggest an alternative OO aproach: define a class for every command you support, i.e.: MyApp::Command::ls, MyApp::Command::cp, etc.
Then, in every command class define the set of methods you need, i.e. parse_args(), run(), print_output(), etc.
And then use the perl OO notation to dispatch commands:
while(read_cmd()} {
eval {
$cmd_name=~/^\w+/ or die "invalid command";
my $cmd_class = "MyApp::Command::$command_name";
my $cmd = eval { require $cmd_class; $cmd_class->new };
if ($@) die "unknow command $cmd_name ($@)\n";
$cmd->parse_args(@args) or die "...";
$cmd->run(@args) or die "...";
etc();
};
print $@ if $@;
}
This way, adding new commands is very easy, just create new classes for them and drop them on the rigth directory. It also allows for simple code reuse via inheritance.
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.