nnvgps has asked for the wisdom of the Perl Monks concerning the following question:

I am trying for thread programming perl in a following way: Description: The current perl script needs to call another subroutine exists in the another perl module(.pm file) using thread method. Case1: general way of callign subroutine using threads is like

my $t= Thread->new(\&process, @args);

where process is the subroutine exists in the same perl file. Case2: calling the subroutine which exists in a different perl module

my $t= Thread->new(\&$anotherfile->another_process, @args);

where another_process exists in the different perl module and not in the same perl file. Question is that Case2 is not working for me. I am not able pass any arguments to this thread. Can any one helpme to solve this issue? Regards Suresh

Replies are listed 'Best First'.
Re: How to call the subroutine from another perl module using threads
by Eily (Monsignor) on Jun 26, 2013 at 12:36 UTC

    You have to import the module first to be able to access its subs: use AnotherMod qw/parameters/; and then you can get your sub reference either with the package name like Thread->new(\&AnotherMod::subName, @args); or without like Thread->new(\&subName, @args); depending on whether your module exported those subs into your caller's namespace or not.

    Edit: I still should think twice about what I wrote, you use a module, not import

Re: How to call the subroutine from another perl module using threads
by Anonymous Monk on Jun 26, 2013 at 23:58 UTC

    I am trying for thread programming perl in a following way

    Don't use Thread anymore, use threads instead

    Case2: calling the subroutine which exists in a different perl module my $t= Thread->new(\&$anotherfile->another_process, @args);

    That has very little chance of being correct. What is the return value of $anotherfile->another_process?

    I think you should read threads synopsis and Simple Module Tutorial and try maybe

    threads->create{ sub { $object->method( @args ) }, ); threads->create{ sub { $object->method( @_ ) }, @args );