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


In reply to Re: Automating dispatch tables by davido
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":



  • 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.