ajeet@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks

Again here to seek solution from you...

This time i am facing a problem with return type of threads->create().

Situation is smoething like this....Suppose I have two functions "A" and "B". Now function "A" calls function "B", Now the function "B" is written to create threads, for ex.  my $returnVal=threads->create(&threadFunc). Now I am returning $returnVal from function "B" to "A". Function "A" is storing this return value in variable $thrdObj. Now when I call $thrObj->tid(), it throws an error like Thread 1 terminated abnormally: Can't call method "tid" without a package or object reference .

I just want to call tid() function of threads in function "A".

Please Help me out...

Thanks in Advance...

Replies are listed 'Best First'.
Re: Return Type of threads->create
by Corion (Patriarch) on Apr 19, 2010 at 09:02 UTC

    Please show us code that we can run that exhibits the problem you encounter. The error message means that $thrObj has undef as value when you call $thrObj->tid(). So somewhere further up in the call chain, you are losing the value.

Re: Return Type of threads->create
by cdarke (Prior) on Apr 19, 2010 at 10:56 UTC
    my $returnVal=threads->create(&threadFunc)

    does not appear to be correct, you are calling 'threadFunc' and using its return value in threads->create as the function name/ref. This is probably why $returnVal is undef, although I would have thought that you would check that in your code. To quote the threads documentation:
    FUNCTION may either be the name of a function, an anonymous subroutine +, or a code ref. my $thr = threads->create('func_name', ...); # or my $thr = threads->create(sub { ... }, ...); # or my $thr = threads->create(\&func, ...);
      Thank You cdarke... You are right, got my problem resolved... :)