#! perl -l use strict; use warnings; use threads; $|++; sub thread{ my $tid = threads->tid; my $fn = "file" . $tid; open(my $fh, ">", $fn) or die "Couldnt open filehandle $!"; my $pid = open(my $fs, "-|", "perl test.pl") or die "Couldnt open process: $!"; print $fh $_ while <$fs>; close($fs); close($fh); } my $thr1 = threads->create(\&thread); sleep 1; unlink("file1") or print "Cant remove file1 because $!"; my $thr2 = threads->create(\&thread); sleep 1; unlink("file2") or print "Cant remove file2 because $!"; $thr1->join(); unlink("file1") or print "Still cant remove file1 because $!"; $thr2->join(); unlink("file1") or print "This isnt printed $!"; unlink("file2") or print "This isnt printed $!"; #### sleep 5;