in reply to Flow control / case structure

One way to do this is with dispatch table:

my %actions = ( Register => \&handle_register, "Book Course"=> \&handle_book_course, # ... ); # ... if (defined $actions{$action}) { $actions{$action}->(); } else { print "No such action $action..."; }

Basically you have a hash giving a subref for each action. You can even write an anonymous subroutine in the dispatch table using the sub { ... code here ... } syntax. This should be pretty fast and allow you to easily add or remove actions, and have one action be callable by multiple names.

Note that calling a subroutine named by the action argument is a major security risk if you don't at least run checks to see if it's valid first.

Replies are listed 'Best First'.
Re: Re: Flow control / case structure
by demerphq (Chancellor) on Sep 08, 2001 at 02:53 UTC
    An interesting twist on this idea is to put the 'dispatcher' part of the dispatch table concept into AUTOLOAD. You execute your 'actions' as method calls against an object. When perl doesnt find it it calls the dispatcher from AUTOLOAD and installs the subroutine from the dispatch table. This method may actually be a little insecure, depending on how you built your objects. You might want to use an special class, with no additional methods (even new) so someone can't call your personal routines for instance.

    Another idea, more secure, is to use Perls class heirarchy as the dispatch function. Each action gets translated into a class name and then in an eval used and new()ed. The returned object is then manipulated as so desired. If you prepend a 'safe' root name to the class and the appropriate directory is secure then overall security shouldn't too much of an issue. Also a bit of s///g ing of $action first might be in order.

    #untested sub handle_action { my ($self,$action,$value)=@_; my $obj=eval " use Some::Class::$action; Some::Class::$action->new() "; #eval returns the last value evaluated return $self->raiserror($action,$value,$@) unless $obj->is("Some::Class::Proto"); $obj->exec($value); }
    well anyways those are some of the more unusual ways. :-) I like the class heirarchy the best in some ways cause it neatly modularizes the whole process.

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)