Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I map UIDs to user names using Perl library functions?

Tags:

unix

perl

I'm looking for a way of mapping a uid (unique number representing a system user) to a user name using Perl.

Please don't suggest greping /etc/passwd :)

Edit

As a clarification, I wasn't looking for a solution that involved reading /etc/passwd explicitly. I realize that under the hood any solution would end up doing this, but I was searching for a library function to do it for me.

like image 311
Mike Avatar asked Jan 22 '23 00:01

Mike


1 Answers

The standard function getpwuid, just like the same C function, gets user information based on its ID. No uses needed:

my ($name) = getpwuid(1000);
print $name,"\n";

Although it eventually reads /etc/passwd file, using standard interfaces is much more clear for other users to see, let alone it saves you some keystrokes.

like image 198
P Shved Avatar answered Jan 29 '23 08:01

P Shved