my @portlist = qw/10001 10002 10003 10004/; use Parallel::ForkManager; $pm = Parallel::ForkManager->new(scalar @portlist); foreach my $target (@targets) { my $pid = $pm->start and next; my $port = pop @portlist; system("telnet $target $port"); push @portlist, $port; $pm->finish; # Terminates the child process }