想要理解@ perldoc.perl.org 给出的示例代码行对于 getlogin
$login = getlogin || getpwuid($<) || "Kilroy";
它似乎试图从 getlogin
获取用户名或 getpwuid
, 但如果其中任何一个失败,请使用 Kilroy
反而。我可能错了,所以请纠正我。另外,我一直在使用 getlogin()
在以前的脚本中 - getlogin()
之间有什么区别吗?和 getlogin
?
这段代码要防止什么?另外,$<
的目的是什么?服务?在查找 $<
时,我不确定要搜索什么是什么以及它的作用。
编辑
在特殊变量部分找到这个 - 仍然不知道为什么需要它或者上面的例子中做了什么
$<
The real uid of this process. (Mnemonic: it's the uid you came from, if you're running setuid.) You can change both the real uid and the effective uid at the same time by using POSIX::setuid(). Since changes to $< require a system call, check $! after a change attempt to detect any possible errors.
编辑 x2
这条线与上面的例子有可比性吗? (目前我用它来避免“cron”执行脚本的任何潜在问题——我从来没有遇到过这个问题,但我试图避免任何理论上的问题)
my $username = getlogin(); if(!($username)){$username = 'jsmith';}
最佳答案
你完全正确。如果getlogin
返回 false 它将测试 getpwuid($<)
如果返回 false,它将设置 $login
到“基尔罗伊”
$<
是进程的真实uid。即使您在 setuid 环境中运行,它也会返回进程启动时的原始 uid。
编辑以匹配您的编辑:)
getpwuid
通过 UID 返回用户名(在标量上下文中,这里就是这种情况)。你会想要 $<
作为一个参数,以防程序在某个时候切换 UID($<
是它开始时使用的原始 UID)
关于function - Perl - getlogin、getpwuid 和 $<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5379021/