Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: How to sovle this, if i use strict i'm getting error.

by BUU (Prior)
on Dec 06, 2005 at 09:10 UTC ( #514375=note: print w/ replies, xml ) Need Help??


in reply to Re: How to sovle this, if i use strict i'm getting error.
in thread How to sovle this, if i use strict i'm getting error.

I suppose I'm nitpicking here, but something has always bothered me about this answer (and those like it). Perl already provides a perfectly code "dispatch table", that is, a hash that maps subroutine names to subroutine references! I'm not sure why people are so strongly discouarged from using it. It's one thing to discourage accessing this hash for variables, since that leads to global variables and other such evil, but in this case you're just wasting effort to create something that already exists.


Comment on Re^2: How to sovle this, if i use strict i'm getting error.
Re^3: How to sovle this, if i use strict i'm getting error.
by Corion (Pope) on Dec 06, 2005 at 09:14 UTC

    The point is control. Eval-based dispatch is much like an unstructured goto $EXPR - you cannot really be sure of the program flow. An explicit hash lists all locations where the code is allowed to branch to. This prevents malicious attacks where the attacker could try to make a call to the exit or system core function and it allows much better error handling. It also decouples the subroutine name from the argument, which I find very convenient in web applications.

Re^3: How to sovle this, if i use strict i'm getting error.
by jhourcle (Prior) on Dec 06, 2005 at 09:26 UTC

    They think it's a bad idea because 'use strict' complains ... but if you know that what you're trying to do is correct, you can shut off 'use strict' for the specific complaint that it has:

    use strict; use warnings; for my $i ( 1 .. 2 ) { $a = 'test'.$i; no strict 'refs'; &$a; } sub test1 { print 'test1'; } sub test2 { print 'test2'; }

    So where's the problem with that? None, really. I mean, it doesn't propogate, so you're still protected in the subroutine you're calling:

    use strict; use warnings; for my $a qw( test1 test2 ) { no strict 'refs'; &$a; } sub test1 { print 'test1'; } sub test2 { my $x='test3'; &$x }

    PS. For the original poster -- it's convention that you don't use $a and $b as variable names, unless you're dealing with sorting.

      You don't actually need to change the current strictures:
      use strict; sub foo { print "Foo"; } sub bar { print "bar"; } main->can("foo")->();
Re^3: How to sovle this, if i use strict i'm getting error.
by VSarkiss (Monsignor) on Dec 06, 2005 at 15:34 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-09-23 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls