如何使用哈希在 Perl 中创建回调函数(调度表)?
我想调用一个动态调度其他函数的主控制器函数,如下所示:
package Controller;
my %callback_funcs = ();
sub register_callback{
my ($class,$callback,$options) = _@;
#apppend to %callback_funcs hash ... ?
}
sub main{
%callback_funcs = ( add => 'add_func', rem => 'remove_func', edit => 'edit_func');
while(<STDIN>){
last if ($_ =~ /^\s*$/);
if($_ == 'add' || _$ == 'rem' || _$ == 'edit'){
$result = ${callback_funcs['add']['func']}(callback_funcs['add']['options']);
}
}
}
sub add_func{
...
}
一个警告是子函数是在其他模块中定义的,因此回调必须能够引用它们......加上 我很难得到正确的哈希值!
I want to call a main controller function that dispatches other function dynamically, something like this:
package Controller;
my %callback_funcs = ();
sub register_callback{
my ($class,$callback,$options) = _@;
#apppend to %callback_funcs hash ... ?
}
sub main{
%callback_funcs = ( add => 'add_func', rem => 'remove_func', edit => 'edit_func');
while(<STDIN>){
last if ($_ =~ /^\s*$/);
if($_ == 'add' || _$ == 'rem' || _$ == 'edit'){
$result = ${callback_funcs['add']['func']}(callback_funcs['add']['options']);
}
}
}
sub add_func{
...
}
One caveat is that the subs are defined in other Modules, so the callbacks would have to be able to reference them... plus
I'm having a hard time getting the hashes right!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,可以有一个包含可以从标准输入调用的匿名子例程的哈希。
您可以在散列中插入更多散列值:
您可以像这样调用一个散列值
或者
您可以从 STDIN 或其他任何地方获取散列键。
您还可以匿名定义它们,然后引用它们。
但是,我不会调用这些回调。回调是在另一个子例程返回后调用的子程序。
您的代码也充满了语法错误,这可能会掩盖这里更深层次的问题。我也不清楚你想对第二级数组做什么。您何时定义这些子系统?谁在何时使用它们?用于什么目的?
So, it's possible to have a hash that contains anonymous subroutines that you can invoke from stdin.
And you can insert more hashvalues into the hash:
And you would invoke one like this
Or
You can get the hashkey from STDIN, or anywhere else.
You can also define them nonymously and then take a reference to them.
I wouldn't call these callbacks, however. Callbacks are subs that get called after another subroutine has returned.
Your code is also rife with syntax errors which may be obscuring the deeper issues here. It's also not clear to me what you're trying to do with the second level of arrays. When are you defining these subs, and who is using them when, and for what?
也许这个简化的例子会有所帮助:
Perhaps this simplified example will help:
关于如何在单个文件中构建调度表并通过它调用函数,您已经得到了一些很好的答案,但您还一直在谈论希望在其他模块中定义函数。如果是这种情况,那么根据每个模块所说的可分派功能动态构建分派表,而不是担心手动保持最新状态不是更好吗?当然会!
当然,演示这一点需要多个文件,我正在使用
dispatch_core.pl:DTable/Add.pm:DTable/MultDiv.pm
添加
:
然后,在命令行上:
新函数现在就像使用适当的
dispatchable
将新文件放入DTable目录一样简单子。无需再次触摸dispatch_core.pl
即可添加新功能。编辑:为了回答关于是否可以在没有 Module::Pluggable 的情况下完成此操作的评论问题,这里有一个修改后的dispatch_core.pl,除了定义可分派函数的模块之外,它不使用任何外部模块:
You've already got some good answers on how to build a dispatch table and call functions through it within a single file, but you also keep talking about wanting the functions to be defined in other modules. If that's the case, then wouldn't it be better to build the dispatch table dynamically based on what dispatchable functions each module says it has rather than having to worry about keeping it up to date manually? Of course it would!
Demonstrating this requires multiple files, of course, and I'm using Module::Pluggable from CPAN to find the modules which provide the function definitions.
dispatch_core.pl:
DTable/Add.pm:
DTable/MultDiv.pm:
Then, on the command line:
Adding new functions is now as simple as dropping a new file into the DTable directory with an appropriate
dispatchable
sub. No need to ever touchdispatch_core.pl
just to add a new function again.Edit: In response to the comment's question about whether this can be done without Module::Pluggable, here's a modified dispatch_core.pl which doesn't use any external modules other than the ones defining the dispatchable functions: