Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Re: Re: Automating dispatch tables by dragonchild
in thread Automating dispatch tables by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found