Perl线程-从模块(pm)调用子例程

xeroxed_yeti

启动额外的perl模块中定义的子例程的线程的正确语法是什么?

perl程序:

use strict;
use warnings;
use forks;
require testModule;

# before solution - thanks ysth!
# testModule->main will not work!
#my $thr1 = threads->new(\&testModule->main, "inputA_1", "inputB_1");
#my $thr2 = threads->new(\&testModule->main, "inputA_2", "inputB_2");

# solved
#my $thr1 = threads->new(\&testModule::main, "inputA_1", "inputB_1");
#my $thr2 = threads->new(\&testModule::main, "inputA_2", "inputB_2");
my @output1 = $thr1->join;
my @output2 = $thr2->join;

perl模块testModule.pm:

package testModule;
sub main{
    my @input = @_;
    #some code
    return ($output1, $output2)
}

testModule-> main的确切系统调用是什么?

提前致谢!

口渴

您几乎完全正确:

...threads->new( \&testModule::main, "inputA_1", "inputB_1" );

->仅用于类/实例方法调用;如果您希望将其作为类方法调用(它将@input获得类名以及“ inputA_1”和“ inputB_1”),则可以执行以下操作:

...threads->new( sub { testModule->main(@_) }, "inputA_1", "inputB_1" );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章