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. |