in reply to How do I run subroutines in parallel?

What you want is threads. Here are an example of how to do this. Note, this script is only for Perl-5.8.0, don't use other version. I recommend that you compile Perl-5.8.0 without PerlIO, because if you are using sockets with PerlIO you will get a lot of bugs! PerlIO is very good, but now it has some bugs to solve (see the perlbug list), we need to wait for Perl-5.8.1 or get the last pathches.

This example show how to use the share resource of threads. If you want a variable that can be shared by the other threads, just declare it like the example, with : share in the end, don't forget to load the module threads::shared!

Other thing, note that the main is another thread too! If it goes out (exit), the other threads will be closed too. To wait a thread use the command join: $thread->join

#!/usr/bin/perl use threads; use threads::shared; $|=1; my ($global) : shared ; my $thr1 = threads->new(\&TEST,1) ; my $thr2 = threads->new(\&TEST,2) ; my @ReturnData = $thr1->join ; print "Thread 1 returned: @ReturnData\n" ; my @ReturnData = $thr2->join ; print "Thread 2 returned: @ReturnData\n" ; ######## # TEST # ######## sub TEST { my ( $id ) = @_ ; for(0..10) { $global++ ; print "id: $id >> $_ >> GLB: $global\n" ; sleep(1) ; } return( $id ) ; }

Graciliano M. P.
"The creativity is the expression of the liberty".