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...
In reply to Re^2: method dispatch question
by rovf
in thread method dispatch question
by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |