If you are trying to get the username who is running the CGI execution, you can use the following.
my $username = $ENV{LOGNAME};
or you can combine multiple options to make sure one of them return a valid data.
my $username = $ENV{LOGNAME} || getpwuid($<) || $ENV{USER};