btrott and mikkoh both present the two best solutions to solving the problem. It all depends on how dynamic you want your code to be. If you have several predefined functions and $a can only be a handful of them I would go for setting $a to the function and then calling it by &$a because this will get you the fastest execution time.

However, if you want $a to be truly dynamic you can use eval to execute the contents of $a, but keep in mind that each time you run it perl has to recompile the eval.

Essentially if you want perl to only compile the function once and you have a couple predefined functions I think setting $a to a function is the way to go, otherwise I guess taking the performance hit from eval and recompiling the code every time $a is executed would be the way to go.

Hopefully this helps give you an idea as to why you would want to use either of the two ways.

Otherwise, if you're looking to do something *really* wackey you could use evals to create static functions ala

eval '$a = sub { if($joe){somesub($arg1,$arg2);} }'
This way you can get the performance boost of executing $a through &$a but still have dynamic code from eval.

In reply to Re: Dynamic Code? by gaspodethewonderdog
in thread Dynamic Code? by kael

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.