I've never used that module, but I've got a few ideas. Hopefully I'm not too off base with them.
Here's my guess on what may be happening. In your code, you're trying to delete the userAccountControl attribute and then add it back in with a value. Active Directory is probably considering this to be a required and/or protected attribute and is throwing an error when you try to delete it.
After taking a quick peek at the Net::LDAP module's documentation, I'd recommend trying to use 'replace' method instead of the 'delete' and 'add' combo. In other words, something like:
$res = $ldap->modify ($distinguishedName,
replace => {userAccountControl => "514"}
};
I'm not guaranteeing that this will for sure work, but it seems to make sense in my mind at least.