I see in your script that you are trying to generate for your users random passwords.
I would like to recommend to you this module: Crypt::GeneratePassword.
I have found it to be quite useful. Here is a snippet of code to demo it:
use Crypt::GeneratePassword qw(word chars);
my $word = chars($minlen,$maxlen);