Trust me on this: if you organize your code the way revdiablo suggested, no one will have trouble understanding what's going on. People who are still somewhat new to perl will most likely learn a lot from looking at such code, but I don't think they'll have trouble understanding it. You could actually put the "%commands" declaration (with all the sub definitions) in a separate source file, and just "require" that file in your main script -- on the whole, if you can "objectify" and "modularize" your scripting, you'll be better off (and so will anyone who inherits the code from you).

As for defining the CLI grammar, you could simplify things a lot by sticking with single-word commands; e.g.:

time # with no args, command means "show time" time arg [...] # with args, it means "set time"
and so on. By writing the command handler functions to work this way, you'll need fewer distinct commands, which will be nice for everybody involved. So it could look like this:
while (<STDIN>) { my ( $cmd, @args ) = split; if ( exists( $command{$cmd} )) { $command{$cmd}->( @args ); else { $command{default}->(); } }

One more suggestion: you might look into Term::Readline, to enable various nifty unix-shell-like behaviors (command line editing, command history and recall, tab completion of commands and file names, etc).

Update: forgot to suggest how some functions might be written:

my %command = ( time => sub { if ( @_ == 0 ) { print "time is ", scalar localtime, $/; } else { print "let's pretend the time is @_\n"; } }, date => sub { if ( @_ == 0 ) { print "You don't have a date for tonight\n"; } else { print "Okay, we'll see if @_ will go out with you\n"; } }, # ... and so on (updated to remove undeclared array) );

In reply to Re^7: CLI using hashes by graff
in thread CLI using hashes by bahadur

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.