Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Automating dispatch tables

by davido (Cardinal)
on Apr 16, 2004 at 07:58 UTC ( [id://345673]=note: print w/replies, xml ) Need Help??


in reply to Automating dispatch tables

I'm not an expert on any of this, but I'll accept your invitation to meditate out loud.

Your second example could just as easily be written as follows:

sub foo {} sub bar {} sub baz {} sub qux {} if ( my $ref = main->can($method) ) { $ref->(); }

But what your version gains is a new package namespace. This can be a convenient way of giving all of the methods in your "dispatch table" a set of common but private variables to work with. This kind of starts feeling like static variables, except that you can get at them from the outside if you know their names.

On the other hand, this could be nearly as clearly accomplished by putting the sub declarations in an enclosed lexical scope that executes near the top of your script. Example:

{ sub foo{} sub bar{} sub baz{} sub qux{} } my %dispatch = ( foo => \&foo, bar => \&bar, baz => \&baz, qux => \&qux );

In this case, the subs have their own lexical scope to play within. Your second method provides a package for the methods to play in. My method provides a lexical scope. You can do a lot of the same things with that, except that the lexically scoped variables aren't as easily accessible from the outside except by the subs declared within that same scope.

The next issue is the convenience of a hash as a dispatch table. Hash elements can be deleted, placed into other hashes, or handed around all together using a hashref. That's a convenient entity to work with.

Your second method would be that convenient if it were taken one step further, to the point that the refs were captured into a lexically scoped dispatch hash. But then you're getting into another layer of abstraction. Not a big deal. But helpful if you want the convenience that a hash-based dispatch table can give you.

A final issue to consider is to weigh the ramifications of UNIVERSAL::can being mostly incompatible with AUTOLOAD. Just be careful there.


Dave

Replies are listed 'Best First'.
Re: Re: Automating dispatch tables
by BUU (Prior) on Apr 16, 2004 at 08:02 UTC
    Interesting thoughts on the lexical scoping issue, and you're quite correct that a hash can have some advantages. However the main reason I used a different package was simply so you had an easy way of determing which subs were allowed to be called by the dispatch table. If you just define the subs in 'main' you have a potential security flaw, as any sub could be called. (Assuming you were dispatching based on at least mildly untrusted data of course).

    In some cases you do need the power of a hash based dispatch, for instance if you wanted to assign a "security level" required to execute each sub, or complicated things of that nature. But for just doing quick "switch" type dispatches, this way is much faster to type and easier to maintain.
      In some cases you do need the power of a hash based dispatch, for instance if you wanted to assign a "security level" required to execute each sub, or complicated things of that nature. But for just doing quick "switch" type dispatches, this way is much faster to type and easier to maintain.

      Uhhh ... that's not quite right. If you're going to go to all the trouble of actually making a new package and a new file (cause, you might as well make these shareable!), then you'd probably also add a security function somewhere. I would envision something like:

      package dispatch; my %disallowed = ( security => 1, ); sub security { # Handle security here, somehow, returning a boolean. } # Because we're overloading can(), we can easily make it play nicely w +ith AUTOLOAD. sub can { my $proto = shift; my ($method) = @_; return '' if $disallowed{$method}; UNIVERSAL::can($proto, $method); } #### Functions to dispatch to below here --------------- if (dispatch->security( ... ) and my $method = dispatch->can( ... )) { $method->(@args); }

      Now, to correct a misconception - without serious craziness of the Devel:: variety, you cannot access a lexically scoped variable outside its scope. Period, Do Not Pass Go, End Of Story. In other words, it's not an issue. This goes for packages or closures. (That's kinda why they're called "closures".)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://345673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-26 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found