in reply to Trying to get around the "use strict 'refs'" Issue

This is a job for a dispatch table...

#tested my %dispatch = ( INTEGER => \&checkInteger, DATE => \&checkDate, IPADDRESS => \&checkIpaddress, FOO => \&checkFoo ); my $type = "INTEGER"; my $answer = "44"; die "can't do $type" unless exists $dispatch{$type}; my $convAnswer = $dispatch{$type}->($answer); sub checkInteger { print "int" } sub checkDate { print "date" } sub checkIpaddress { print "ip" } sub checkFoo { print "foo" }

If you insist on using your no strict "refs" method it's only a matter of time before someone enters deleteAllFiles as the data type.

Edit: Damn. You're all too fast. Or is it that I'm too Slow?

Replies are listed 'Best First'.
Re^2: Trying to get around the "use strict 'refs'" Issue
by naikonta (Curate) on May 08, 2007 at 14:03 UTC
    If you insist on using your no strict "refs" method it's only a matter of time before someone enters deleteAllFiles as the data type.
    Those are two different matters, and yet can be related. Strictures is about enforcing dicipline on variable declarations and/or explicit namespace usage, hard references, and bareword misplace/misuse. But use strict; gives us no guarantee about malicious user input prevention. We need another step for that. And this step is independent of strict.

    Nevertheless, for me, strict and warnings are just too valuable to be put aside. I wouldn't go and code without them, even if I were at the same level as merlyn, except probably in one-liners.

    Damn. You're all too fast. Or is it that I'm too Slow?
    Don't worry, it's not about speed* in aswering :-) But rather, IMO, it's about correctness, accuracy, and relevancy**, well, as far as "the right question" is concerned. I personally consider your effort has met the three.

    * Of course, answering a question with the same solution as other previous answers in the same thread after a significant time potentially invites something bad.

    ** I was thinking about something regarding my own "bads" but couldn't recall, and I said this to remind myself. Now I remember two of them, hence this second note


    Update: Added the second footnote

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re^2: Trying to get around the "use strict 'refs'" Issue
by qazwart (Scribe) on May 07, 2007 at 17:54 UTC
    Seems like a dispatch table is the way to go.

    To use the dispatch table, I'll have to modify both the table and add the subroutine instead of just adding a new routine to my program

    However, as you pointed out, what if someone defined a nefarious routine? My program would simply do it's little check, notice the "checkCauseMayhem" and go on its merry way.

      However, as you pointed out, what if someone defined a nefarious routine?

      If someone malicious has access to your computer so as to be able to modify the code of any program you deem important, you have greater worries than said person modifying the code of any program you deem important.

      However, as you pointed out, what if someone defined a nefarious routine? My program would simply do it's little check, notice the "checkCauseMayhem" and go on its merry way.

      That's just the same with your previous approach. The only difference being that in one case you use a generic hash and make it specialized for this task. In the other one, a very specific one -the stash-, designed to do something completely different.