2014-05-19 73 views
2

启动额外perl模块中定义的子例程的线程的正确语法是什么?Perl线程 - 从模块调用子程序(pm)

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->主要

在此先感谢!

+0

我向溶液添加到Perl的代码 - 谢谢你! –

回答

2

你几乎得到它的权利:

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

->仅供类/实例方法调用;如果你希望它被称为一个类的方法(这将使@input获取类的名称,以及“inputA_1”和“inputB_1”),那么你会怎么做:

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

谢谢,这是简单的;) –