Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Threads Calling Subroutine from different PM File

by anshumangoyal (Scribe)
on Nov 26, 2012 at 11:34 UTC ( #1005621=perlquestion: print w/ replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I am using Threads to call a sub-routine from a different PM File. My Code is like this:
use strict; use MyModule.pm use threads; # There is a sub-routine Execute() in MyModule.pm my $arg1 = "argument1"; my $arg2 = "argument2"; my $thr = threads->new(??????, $arg1, $arg2);
How should I call my subroutine here:
MyModule::Execute($arg1, $arg2)
using threads.

Comment on Threads Calling Subroutine from different PM File
Select or Download Code
Re: Threads Calling Subroutine from different PM File
by BrowserUk (Pope) on Nov 26, 2012 at 11:55 UTC
    my $thr = threads->new( \&MyModule::Execute, $arg1, $arg2 );

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Ok, then how will this work:
      $class = MyModule::MySubModule;
      and I want to call SubRoutine from $class.
      my $Thread = threads->new( \&$class::SubRoutine, $arg1, $arg2 );
      This is giving error for wrong syntax.
        Ok, then how will this work: $class = MyModule::MySubModule;

        The obvious answer to that question is: It won't work.

        This is giving error for wrong syntax.

        Unsurprising. If your code isn't syntactically correct, you will get an error.

        Is there a subroutine called MySubModule in the package MyModule?

        If not, what do think $class will end up containing?

        The lesson here is that if you make stuff up when asking a question, instead of showing the actual problem code; the answers you get probably won't work for your actual code. So stop being ... and ask the real question you want answered!


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Re: Threads Calling Subroutine from different PM File
by Anonymous Monk on Nov 26, 2012 at 17:16 UTC

    In addition to the previous answer, you can wrap it into an anonymous subroutine, like this:

    my $thr = threads->new( sub { MyModule::Execute($arg1, $arg2) } );

    (but do check that your code does not change the values of $arg1 and $arg2 after that. With proper scoping, that is not hard to do.)

      There is no virtue in doing that. It would be completely pointless.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

        Yes, in this very case, but the times you can't pass arguments to the subroutine, it is invaluable.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005621]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-10-25 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls