in reply to Re: Complex dispatch table
in thread Complex dispatch table

Thanks for the thoughts.
I guess it's not always easy to give tips without knowing what the thing is being used for. I purposely just wanted tips on the code and not its surroundings, but I'll tell you a little bit more anyway.
It's not about external command-line arguments, but commands typed into the 'shell', so I guess Getopt wouldnt help much (unless I can use that internally as well?) and isnt really the sort of syntax I need.
You'll have to enlighten me on 'semantics' I'm afraid, I think I know what you mean with 'syntax' .. ;)

It's actually a Telnet Client for playing muds with.. With commands similar to zMUD, ie '#VAR myvar {2}' '#TRIGGER {sometext} {#colour red}' to mention a couple, this is the stuff and commands that are getting parsed. I'm already using Parse::RecDescent to parse complex scripts, but am trying to avoid using it for simple commands, which seems to be working so far.

C.

Replies are listed 'Best First'.
Re: Re: Re: Complex dispatch table
by dash2 (Hermit) on Feb 20, 2003 at 11:29 UTC
    I don't know if you can use Getopts modules on a string, but I bet there's an option somewhere. Take a look.

    Semantics means "meaning", more or less. The semantics of a statement are going to be what it actually does. The syntax is just how the statement is structured. So you have to think about how the commands you are using are related. Perhaps some commands have a context (a "state"). Some commands might be "like" others (so you can use inheritance to specify their semantics: derive behaviour from a base class, modify it in the child class). Et cetera.

    At the moment, you are basically assuming "all commands are semantically different" - they each get their own subroutine or module method call. You're also assuming "all commands are syntactically the same", but only in that they can be parsed by regexes. You could increase the level of syntactic sameness, and this might save you time typing out different regexes and format messages.

    Here's a rough example:

    package main; # ... my $command; eval { $command = CommandFactory->create ($input_string); # returns the r +ight subclass of Command }; #trap non-existent commands if ($@) {printandprompt("command not recognized");} if ($command->parse_syntax) { # check for syntax errors eval { $command->execute; }; # trap semantic errors, e.g. "user doesn't exist" if ($@) { print $@; # maybe die if need be } } else { printandprompt $command->syntax_string; } package CommandFactory; sub create { my $class = shift; my $str = shift; my ($cmd, @args) = split $str; my $cmd_class = "Command::" . ucfirst ($cmd); return $cmd_class->new(@args); } package Command; use vars qw/$SYNTAX/; # ... sub new { my $class = shift; my @args = @_; my $self = {args => \@args}; bless $self, $class; } sub _syntax { my $self = shift; no strict 'refs'; my $class = ref ($self) || $self; return ${"$class::SYNTAX"}; } sub parse_syntax { my $self = shift; # check $self->{args} against $self->_syntax # then parse the arguments into $self fields # return false on error # alternatively "die" and catch the exception } package Command::Msg; use Command; use base 'Command'; use vars qw/$SYNTAX/; $SYNTAX = { options => { player => { required => 1, arg => 'string', } loudness => { required => 0, arg => 'int' } } arguments => { min => 1, } }; sub execute { my $self = shift; # send $self->{msg} to $self->{player} with $self->{loudness} }

    dave hj~

      That looks interesting .. Thanks..

      C.

Re: Re: Re: Complex dispatch table
by Thelonius (Priest) on Feb 20, 2003 at 12:42 UTC
    so I guess Getopt wouldnt help much (unless I can use that internally as well?)
    Untested code ahead (must take daughter to school):
    use Text::ParseWords; use GetOpt::Std; @ARGV = shellwords($textin); my $cmd = shift @ARGV; my %opts; getopts('oif:', \%opts);