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

I'm trying to use Multithreading in the following line of code:
$t=new Thread(\&checkTest);
the line causes an error:
Undefined subroutine &threads::new called at betterAbTest.pl line 16.
why? Thread.pm exists, and it's other methods work (some break because they call Thread::new and that again outputs the same error). and what's "&threads"...?

Replies are listed 'Best First'.
Re: Thread::new causes Undefined subroutine &threads::new
by marto (Cardinal) on May 31, 2007 at 08:20 UTC
    Try
    #!/usr/bin/perl use strict; use warnings; use threads; my $t = threads->create(sub { print("Hello! I am a thread\n"); })->joi +n();
    The method threads->new() is simply an alias for threads->create() so feel free to change that.

    If in doubt check the documentation.

    Hope this helps.

    Martin
Re: Thread::new causes Undefined subroutine &threads::new
by ysth (Canon) on May 31, 2007 at 09:28 UTC
    Thread.pm cheats; it uses the functions in the threads package, but doesn't load threads.pm, instead just loading thread's XS component. This worked great...until the threads module was changed to have threads::new be just an alias to threads::create, with the alias being set in the .pm file, not via XS. And suddenly, if you load Thread but not threads, there is no threads::new function anymore.

    I believe this is only a problem with the CPAN threads module at the moment. The workaround is to use threads;.

Re: Thread::new causes Undefined subroutine &threads::new
by blazar (Canon) on May 31, 2007 at 08:31 UTC

    I'm trying to use Multithreading in the following line of code:

    $t=new Thread(\&checkTest);

    perldoc Thread:

    NAME Thread - manipulate threads in Perl (for old code only)
Re: Thread::new causes Undefined subroutine &threads::new
by chrism01 (Friar) on May 31, 2007 at 23:50 UTC
    As implied by above comments, "Threads" relates to Perl 5.0 threads, whereas "threads" (note case change) is the new model introduced in 5.6 and avail for user from 5.8.
    See Description section here http://perldoc.perl.org/perlthrtut.html
    Basically, the various tutorials/hints I used all said to avoid "Threads".

    Cheers
    Chris