Don't rely on shell, use system functions instead:
my $homedir = (getpwuid($<))[7];.
$< is the real UID of the user who's running the script, and 7'th field of
getpwuid($uid) is the home directory path. See also:
getpwent.
Sorry if my advice was wrong.