Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

calling a sub using its name assigned to a variable ?

( #276138=categorized question: print w/ replies, xml ) Need Help??
Contributed by meetn2veg on Jul 21, 2003 at 04:35 UTC
Q&A  > subroutines


Description:

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.

Answer: calling a sub using its name assigned to a variable ?
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); ...
Answer: calling a sub using its name assigned to a variable ?
contributed by jryan

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};

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (8)
    As of 2014-09-16 23:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (51 votes), past polls