in reply to multiple-arg commands
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: multiple-arg commands
by Ahbeyra (Monk) on Oct 24, 2001 at 23:19 UTC |