PERLANCAR-File-HomeDir
view release on metacpan or search on metacpan
get_users_home_dir.
0.04 2017-01-04 (PERLANCAR)
- Add function: users_home().
0.03 2016-10-07 (PERLANCAR)
- Handle case where getpwuid() is not implemented, e.g. on Windows
(ref:
https://github.com/perlancar/perl-Perinci-CmdLine-Lite/issues/1
).
0.02 2015-04-08 (PERLANCAR)
- Fix OS name for Windows [GH#1].
lib/PERLANCAR/File/HomeDir.pm view on Meta::CPAN
if ($^O eq 'MSWin32') {
# File::HomeDir always uses exists($ENV{x}) first, does it want to avoid
# accidentally creating env vars?
return $ENV{HOME} if $ENV{HOME};
return $ENV{USERPROFILE} if $ENV{USERPROFILE};
return join($ENV{HOMEDRIVE}, "\\", $ENV{HOMEPATH})
if $ENV{HOMEDRIVE} && $ENV{HOMEPATH};
} else {
return $ENV{HOME} if $ENV{HOME};
my @pw;
eval { @pw = getpwuid($>) };
return $pw[7] if @pw;
}
if ($DIE_ON_FAILURE) {
die "Can't get home directory";
} else {
return undef;
}
}
# borrowed from File::HomeDir, with some modifications
sub get_user_home_dir {
my ($name) = @_;
if ($^O eq 'MSWin32') {
# not yet implemented
return undef;
} else {
# IF and only if we have getpwuid support, and the name of the user is
# our own, shortcut to my_home. This is needed to handle HOME
# environment settings.
if ($name eq getpwuid($<)) {
return get_my_home_dir();
}
SCOPE: {
my $home = (getpwnam($name))[7];
return $home if $home and -d $home;
}
return undef;
}
( run in 0.353 second using v1.01-cache-2.11-cpan-8d75d55dd25 )