in reply to a terribly basic question showing my inept skills with regular expressions

Replace this:
$username =~ $1;
with this:
$username = $1;
It'll both untaint the string, and do what is wanted.

There's still lots of superfluous/horrible stuff in there, but at least, it should work now.