Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Creating dispatch table with variable in function name

by dsheroh (Prior)
on Nov 23, 2017 at 09:48 UTC ( #1204120=note: print w/replies, xml ) Need Help??


in reply to Re^3: Creating dispatch table with variable in function name
in thread Creating dispatch table with variable in function name

I see your point (and obviously misunderstood what you were questioning when I posted my earlier reply), but I'm not convinced that the equivalent
my %dispatch = ( first => \&_create_first, last => \&_create_last, user => \&_create_user, id => \&_create_id, email => \&_create_email, create_password => \&_create_create_password, );
is any more virtuous than the map in the OP. Both have the same potential for run-time errors if one or more of the referenced subs doesn't actually exist - simply naming them explicitly does not turn it into a compile-time error:
$ perl -E 'use strict; use warnings; my %d = ( foo => \&foo ); say "OK +"' OK
Retyping _create_ every time doesn't actually buy you anything aside from maybe an eyeball error check. (_create_create_password does look a little suspicious to me, but it might still be correct in the OP's code.)

Replies are listed 'Best First'.
Re^5: Creating dispatch table with variable in function name
by roboticus (Chancellor) on Nov 23, 2017 at 16:46 UTC

    dsheroh:

    I'll call it slightly more virtuous than the map version, and my reasoning is this: I can use grep and find all references to the function(s). On the other hand, if your lookup keys were the same as the function names, it would be a wash IMHO.

    Having said that, in my personal code, I generally use magical code in lots of places, including using map to build dispatch tables as you're doing. But for production code, a little less magic is appreciated when trying to locate things like function references.

    Experimenting with techniques to reduce typing and improve clarity is one of the areas in programming that I find very fun. When we come up with interesting new ways to simplify/clarify some of the menial tasks, we can build new idioms and improve the overall programming experience.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re^5: Creating dispatch table with variable in function name
by Laurent_R (Canon) on Nov 23, 2017 at 11:16 UTC
    Yes, dsheroh++, I agree: quite possibly it isn't much more virtuous, and yes, you might still very well refer to a sub that does not exist and the compiler won't tell you about it.

    On the other hand, it is much easier to check that all your subrefs correspond to actual subs when they are explicitly listed in the dispatch table as in your example than when their names are dynamically created, and it is also probably easier to make sure that your tests cover all of them.

    Also, if we insist that symbolic references for package variables should be avoided (at least for "usual" programs, I am not talking here of pieces of white magics introduced for extending the language), then we should presumably also try to avoid similar constructs for subroutines.

    As I said already, I am not saying that this is bad, I am just asking monks for their opinions on this subject. I have actually done similar things a couple of times, and even explicitly written things to the symbol table in order to extend the language (by replacing a subroutine by another one). I don't think I would do that for regular plain-vanilla coding.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1204120]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-06-05 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?