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

calling a sub using its name assigned to a variable ?

by meetn2veg (Scribe)
on Jul 21, 2003 at 04:35 UTC ( [id://276138]=perlquestion: print w/replies, xml ) Need Help??

meetn2veg has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

Quickly, I'm hoping to be able to do the following; depending upon data stored in a file, I'm looking to call a subroutine if the value of a variable of the same name = 1.

Is this possible? Is there an easier/more efficient way?

A slightly longer version:
data file contains the following:

main_menu:1:1 'main_menu' is the name of the subroutine 1:1 refers to screen position(0=left 1=right) and hide/display.

This data is read by sub build_menus. Simply using 'if' statements, a series of menu 'blocks' are built up to be displayed on screen.

I had been using static files with straight HTML, but nor wequire that the links which are built dynamically contain other veriables appended to the URL (eg: $language, $menu_option, etc...) and am thinking of using a sub routine to build the URLs - rather than using a substitution script to parse the URL after it's been built.

Hope this helps so that someone can help me.

Kind regards from Andorra
Richard.

Originally posted as a Categorized Question.

  • Comment on calling a sub using its name assigned to a variable ?

Replies are listed 'Best First'.
Re: calling a sub using its name assigned to a variable ?
by NetWallah (Canon) on Jul 21, 2003 at 05:28 UTC
    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); ...
Re: calling a sub using its name assigned to a variable ?
by jryan (Vicar) on Jul 21, 2003 at 20:38 UTC

    As NetWallah notes, you want to use a dispatch table, as it allows you to check for the existance of the sub before you call it (one of the "various valid reasons relating to maintainability and security"). However, there is no need to have to deal with such annoying syntax. Check out Exporter::Dispatch on the CPAN. It will make your life much easier. For instance, NetWallah's sample now becomes even cleaner. First the table definition: (with the prototypes removed, as they are pointless in this situation)

    package Options; use Exporter::Dispatch; sub main_menu { ... } sub other_menu { ... }

    And then the call (with the addition of an existance check):

    my $var = ...; my %dispatch_sub = create_dptable Options; $dispatch_sub{$var}->($screenpos1, $screenpos2) if exists $dispatch_su +b{$var};

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-16 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found