#!/usr/bin/perl use strict; use warnings; use threads; foreach (0..100){ # create a thread to call the "run_system" subroutine # to run "ls -la" on the OS my $thread = threads->create("run_system", "ls -la"); $thread->detach; } sub run_system{ my $cmd = shift; system($cmd); } __END__ #1liner perl -Mthreads=async -le"async{ system 'dir'; } for 1 .. 100" #### #!/usr/bin/perl use warnings; use strict; use threads; # join() does three things: it waits for a thread to exit, # cleans up after it, and returns any data the thread may # have produced. my $thr1 = threads->new(\&sub1); my $ReturnData1 = $thr1->join; print "Thread1 returned @$ReturnData1\n"; my $thr2 = threads->new(\&sub2); my $ReturnData2 = $thr2->join; print "Thread2 returned @$ReturnData2\n"; my $thr3 = threads->new(\&sub3); my $ReturnData3 = $thr3->join; print "Thread3 returned @$ReturnData3\n"; sub sub1 { print "In thread1.....\n"; sleep 5; my @values = ('1a','1b', '1c'); return \@values; } sub sub2 { print "In thread2.....\n"; sleep 5; my @values = ('2a','2b', '2c'); return \@values; } sub sub3 { print "In thread3.....\n"; sleep 5; my @values = ('3a','3b', '3c'); return \@values; }