while ($line = $sth->fetchrow_array()) { #Your code goes here my $new_thread = threads->new(\&scan_domain, $line); } sub scan_domain { my $domain_name = shift; print "started thread for $domain_name\n"; my @list =`nmap $line`; foreach(@list){ if($_=~/open/g){ $_ =~ s/\/.*//g; if($myport) {$myport=$myport.','.$_;chomp $myport;} else{$myport=$_; chomp $myport} }