Wanted to understand the example line of code given @ perldoc.perl.org for getlogin
$login = getlogin || getpwuid($<) || "Kilroy";
It seems like it tries to get the user name from getlogin
or getpwuid
, but if either fails, use Kilroy
instead. I might be wrong, so please correct me. Also, I've been using getlogin()
in previous scripts - is there any difference between getlogin()
and getlogin
?
What is this code safeguarding against? Also, what purpose does $<
serve? I'm not exactly sure what to search for when looking up what $<
is and what it does.
$<
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.
Is this line comparable to the above example? (it is currently what I use to avoid any potential problems with "cron" executing a script - i've never run into this problem, but i am trying to avoid any theoretical problem)
my $username = getlogin(); if(!($username)){$username = 'jsmith';}
You're exactly right. If getlogin
returns false it will test getpwuid($<)
if that returns false it will set $login
to "Kilroy"
$<
is the real uid of the process. Even if you're running in a setuid environment it will return the original uid the process was started from.
Edit to match your edit :)
getpwuid
returns the user's name by the UID (in scalar context, which would be the case here). You would want $<
as an argumnent in case the program switched UID at some point ($<
is the original one it was started with)