sub addressExists { my $address=shift @_; my $sql = 'SELECT * FROM emails WHERE `email`=?'; my $sth = $dbh->prepare( $sql ); $sth->execute($address); } #### #open connection # some code here and then: while (my $address=@address) { &addressExists($address); # other stuff } #close connection