sub whoami { for ($ENV{USER}, $ENV{LOGNAME}, getlogin()) { if (getpwnam($_) == $<) { ## is this a matching uid? return $_; ## return that name } } return scalar getpwuid($<); ## fallback, return name for this uid }