In general, it's a bad idea to hard-code usernames/passwords in your scripts. Here are some ways to automatically discover the username/password to use.

Whenever possible, we focus on pulling the information from standard locations, locations where the username/pass is stored for other tools.

MySQL — pull from the ~/.my.cnf

my $db = DBI->connect("DBI:mysql:mysql_read_default_file=$ENV{HOME}/.my.cnf") or die;

The ~/.my.cnf file is just an INI file, so any INI parser will do it. But there are specialized bits of parsing code too:

Informix

Oracle