Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Automating dispatch tables

by BUU (Prior)
on Apr 16, 2004 at 08:02 UTC ( [id://345675]=note: print w/replies, xml ) Need Help??


in reply to Re: Automating dispatch tables
in thread Automating dispatch tables

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.

Replies are listed 'Best First'.
Re: Re: Re: Automating dispatch tables
by dragonchild (Archbishop) on Apr 16, 2004 at 11:52 UTC
    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://345675]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-23 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found