How do I get the current user in Perl in a portable way?

陌路散爱 提交于 2019-11-30 17:42:50

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".

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

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

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

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!