如何以便携方式获取Perl中的当前用户?

时间:2022-11-27 07:22:30

How does one get the current user in a portable way?

如何以便携方式获得当前用户?

This seems like an FAQ but perlport doesn't speak about it, maybe because some odd systems don't have the concept of "user" to being with? However, let's stick to *nix and Windows.

这看起来像是常见问题,但是perlport并没有谈论它,也许是因为一些奇怪的系统没有“用户”的概念?但是,让我们坚持使用* nix和Windows。

getpwuid($>) is not implemented on Windows.

getpwuid($>)未在Windows上实现。

$ENV{USER} || $ENV{USERNAME} seems finicky.

$ ENV {USER} || $ ENV {USERNAME}似乎挑剔。

http://search.cpan.org didn't turn up much.

http://search.cpan.org没有发现太多。

2 个解决方案

#1


19  

getlogin:

getlogin:

This implements the C library function of the same name, which on most systems returns the current login from /etc/utmp, if any. If null, use "getpwuid".

这实现了同名的C库函数,在大多数系统上,它返回/ etc / utmp中的当前登录名(如果有的话)。如果为null,请使用“getpwuid”。

$login = getlogin || getpwuid($<) || "Kilroy";

Do not consider "getlogin" for authentication: it is not as secure as "getpwuid".

不要将“getlogin”视为身份验证:它不像“getpwuid”那样安全。

You can also try ||-ing this with POSIX::cuserid() and Win32::LoginName().

您也可以尝试使用POSIX :: cuserid()和Win32 :: LoginName()进行此处理。

#2


4  

Win32::LoginName() can be used on Windows to retrieve the user name (without the domain name, so it may be ambiguous):

可以在Windows上使用Win32 :: LoginName()来检索用户名(没有域名,因此可能不明确):

use Win32;
my $username = Win32::LoginName;

Win32::pwent implements getpwuid() and other features to query the user database. Unfortunately, it failed to install on my StrawberryPerl 5.12.

Win32 :: pwent实现了getpwuid()和其他功能来查询用户数据库。不幸的是,它无法安装在我的StrawberryPerl 5.12上。

#1


19  

getlogin:

getlogin:

This implements the C library function of the same name, which on most systems returns the current login from /etc/utmp, if any. If null, use "getpwuid".

这实现了同名的C库函数,在大多数系统上,它返回/ etc / utmp中的当前登录名(如果有的话)。如果为null,请使用“getpwuid”。

$login = getlogin || getpwuid($<) || "Kilroy";

Do not consider "getlogin" for authentication: it is not as secure as "getpwuid".

不要将“getlogin”视为身份验证:它不像“getpwuid”那样安全。

You can also try ||-ing this with POSIX::cuserid() and Win32::LoginName().

您也可以尝试使用POSIX :: cuserid()和Win32 :: LoginName()进行此处理。

#2


4  

Win32::LoginName() can be used on Windows to retrieve the user name (without the domain name, so it may be ambiguous):

可以在Windows上使用Win32 :: LoginName()来检索用户名(没有域名,因此可能不明确):

use Win32;
my $username = Win32::LoginName;

Win32::pwent implements getpwuid() and other features to query the user database. Unfortunately, it failed to install on my StrawberryPerl 5.12.

Win32 :: pwent实现了getpwuid()和其他功能来查询用户数据库。不幸的是,它无法安装在我的StrawberryPerl 5.12上。