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

时间:2014-05-19 13:41:48

标签: multithreading perl

启动在额外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 的确切系统调用是什么?

提前致谢!

1 个答案:

答案 0 :(得分:2)

你几乎做对了:

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

->仅用于类/实例方法调用;如果你希望它被称为类方法(这会使@input获得类名以及" inputA_1"和" inputB_1"),那么你会做:

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