while () { chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr] ); next unless $dn; # Make sure something is returned while( my $entry = $dn->pop_entry() ) { $result->write_entry($entry); if ( $result != /dn:/ ) { print OUT (@lines); } while () { ... #### while () { chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr] ); next unless $dn; # Make sure something is returned while( my $entry = $dn->pop_entry() ) { $result->write_entry($entry); if ( $result != /dn:/ ) { print OUT (@lines); } while () { ... #### my %manager; while () { $manager{ $_ } = $ldap->search(...); # return only the manager DN }; while () { ... # you never seem to use the information from MGR here... };