use Net::LDAP::Util qw(ldap_error_text ldap_error_name) ; my $msg ; # you will need it several times $msg = $ldap->bind(%your_bind_params) ; if ($msg->is_error) { my $code = $msg->code ; die join "\n",ldap_error_name($code), ldap_error_text($code) ; } #### while (my ($username,$userdata) = each %petra) { my $entry = Net::LDAP::Entry->new(); my $dn = calculate_it_using($username) ; $entry->dn($dn) ; $entry->changetype("add"); $entry->add(@$userdata) ; $msg = $entry->update($ldap) ; if ($msg->is_error) { # die() the same way as before. I'd suggest # to create a subroutine to do the job, e.g. # die_ldap_error($msg->code) } }