Keep It Simple, Stupid | |
PerlMonks |
Re^4: How to map function name to coderef?by GrandFather (Saint) |
on Jan 07, 2012 at 21:40 UTC ( [id://946797]=note: print w/replies, xml ) | Need Help?? |
Actually Ikegami's 'solution' ("shut up and do what I tell you") almost certainly isn't what the OP is looking for, although it is one way of solving the problem that the OP asked about. eyepopslikeamosquito really gave the best answer (in terms of likely best outcome for the OP) suggesting that the OP was asking the wrong question. Using symbolic references in Perl is almost never involved in a "best" solution. And even in the narrow context of the question asked by the OP it's not at all clear that Ikegami's 'solution' solution is best. Consider:
which prints:
If the OP wants the code to die on a bad sub name then turning a blind eye to the symbolic reference is fine. If the OP wants to detect and handle a missing sub then using the can trick is a better solution. If the OP always knows what the name of the sub will be then there is no need for using a symbolic reference at all. But the OP doesn't give us enough information to make that determination. There are few (no?) hard and fast rules about what constitutes a 'best' anything. You would do yourself a service by disabusing yourself of the notion that there is a one true best solution to almost anything.
True laziness is hard work
In Section
Seekers of Perl Wisdom
|
|