Hello Martin
I am by no Means another Perl hacker, but I solved this another way. First let me say that the way you are thinking about this is probably better than what I did, but the real monks will chime in, I am sure.
I put the password in a file, and made sure only the script user can read the file (user rights), and of course read it in from that file. That was just to avoid visible passwords in the script.