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~


In reply to Re: Re: Re: Complex dispatch table by dash2
in thread Complex dispatch table by castaway

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.