Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Answer: calling a sub using its name assigned to a variable ?

by NetWallah (Abbot)
on Jul 21, 2003 at 05:28 UTC ( #276143=categorized answer: print w/replies, xml ) Need Help??

Q&A > subroutines > calling a sub using its name assigned to a variable ? - Answer contributed by NetWallah

Yes - it is possible to do what you ask, and it is also very efficient. However, monks here will strongly recommend against doing it for various valid reasons relating to maintainability and security.

So - let us attempt to resolve your problem the politically correct, and still efficient way. It turns out that this way is possibly easier to understand too. I'm recommending using a hash which contains subroutine references. The code (Untested, unparsed) will look something like this :

sub main_menu($$){ --code to handle main } sub other_menu($$){ --code } my %dispach_sub =( main_menu=>\&main_menu , other_menu=>\&other_menu ); ..after reading the data file into $var, $dispach_sub($var)($screenpos1, $screenpos2); ...

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2016-12-08 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (135 votes). Check out past polls.