我想使用函数引用来动态执行其他包中的函数.
我一直在为这个想法尝试不同的解决方案,但似乎没有任何效果!所以,我想问这个问题,并在尝试这样做时,解决方案有效!但我不确定这是否是正确的方法:它需要手动工作并且有点"hacky".可以改进吗?
用于支持所需功能的包
package Module;
# $FctHash is intended to be a Look-up table, on-reception
# of command.. execute following functions
$FctHash ={
'FctInitEvaluate' => \&FctInitEvaluate,
'FctInitExecute' => \&FctInitExecute
};
sub FctInitEvaluate()
{
//some code for the evalute function
}
sub FctInitExecute()
{
//some code for the execute function
}
1;
Run Code Online (Sandbox Code Playgroud)2.实用程序脚本需要使用函数引用来使用该包
use strict;
use warnings 'all';
no strict 'refs';
require Module;
sub ExecuteCommand()
{
my ($InputCommand,@Arguments) =@_;
my $SupportedCommandRefenece = $Module::FctHash;
#verify if the command is supported …Run Code Online (Sandbox Code Playgroud)