What on earth is your real-world application that needs to do this?

If you refer to the problem of method dispatch per se, the application is not so unusual: We have several "variations" of a method, which can be selected by a key, which gets calculated on run-time. One obvious implementation for this is to have a hash, which maps the keys to the methods. Just a normal dispatch table, only that it is on "class methods" instead of "normal" subs.

Now since Perl does not "really" distinguish between methods and non-methods (it is more like a conceptual idea of the programmer, respectively how to use them), I can of course do it like I would with a function dispatch table, which means that I can put $self explicitly as a first parameter to the dispatched functions, as I have shown in my OP.

But when I encounter a problem like this, I am usually interested in finding other solutions. In this case, I was interested whether a solution would exist which would still allow me to use the syntax $self->... for calling the method, only that to the right of -> is now not the name of the function, but a variable holding the reference to the function. How this is done, was nicely explained by parv.

Having seen how easy this is, I became interested whether one could also omit the auxiliary variable holding the method reference. This turned out not to be so easy.

So, finding ways to put "everything into one single expression", is not so much a need, but curiosity, paired with my experience that by investigating this type of problems, we often learn a lot about the language in question - or at least I do. In the past, more than once a question like this, has brought me insight into new ways to solve things easier...

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Re^2: method dispatch question by rovf
in thread method dispatch question by rovf

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.