if("%{User-Name}" =~ /\\\\?([^@\\\\]+)@?([-[:alnum:]._]*)?$/) {

I'm not that good with POSIX regex, but shouldn't it be
if("%{User-Name}" =~ /\\\\?([^@\\\\]+)@?([-[:alnum:]\._]*)?$/) {

AFAIK the dot "." means "any char" so I think it needs to be escaped.