启动额外 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" );
关于multithreading - Perl 线程 - 从模块调用子例程 (pm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739370/