You just write a parser to do what you want. In the chatterbox, the input field is passed as one big string when the form is submitted. The stuff implementing the chatterbox parses the line and does the right thing.

If you don't have things already split, it depends on how you want to parse things. Going with your chatterbox example, you could always look at the first character of the line to see if it starts with a `/'. If it doesn't default to passing the line to your say routine (or whatever). If it did start with `/', then do something like:

my %handlers = ( tell => \&tell_user, msg => \&tell_user, chatoff => \&toggle_chat, chaton => \&toggle_chat, moo => \&order_cheese, ); my( $command, $rest ) = split( /\s+/, $line, 2 ); $command =~ s{^/}{}; # throw away / if( exists( $handlers{ lc($command) } ) { $handlers{ lc($command) }->( $rest ); } else { send_error( "Unknown command `$command'" ); }

Each of the handlers would be passed the rest of the line to do with as they wish. tell_user would probably do another split to pull off the target and use the rest as the message to send, while order_cheese might take the rest of the line as a type of cheese and the amount to order.

For more inspiration, take a look at how the CPAN.pm module's shell is implemented and how it parses lines.


In reply to Re: multiple-arg commands by Fletch
in thread multiple-arg commands by Ahbeyra

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.