如何以可移植的方式在Perl中获得当前用户? [英] How do I get the current user in Perl in a portable way?

查看:71
本文介绍了如何以可移植的方式在Perl中获得当前用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以一种可移植的方式吸引当前用户?

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

这似乎是一个常见问题解答,但 perlport 并未提及,也许是因为有些奇怪系统没有用户"的概念吗?但是,让我们坚持使用* nix和Windows.

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.

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

getpwuid($>) is not implemented on Windows.

$ENV{USER} || $ENV{USERNAME}似乎很挑剔.

http://search.cpan.org 出现的次数很少.

推荐答案

getlogin :

这实现了同名的C库函数,在大多数系统上,该函数从/etc/utmp返回当前登录名(如果有).如果为null,请使用"getpwuid".

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

请勿将"getlogin"用于身份验证:它不如"getpwuid"安全.

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

您也可以尝试||-使用 POSIX :: cuserid ()和 Win32 :: LoginName ().

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

这篇关于如何以可移植的方式在Perl中获得当前用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆