Get name of user invoking program?

J

Justin C

Probably an FAQ. I need to get the user name of who has invoked the
program. perldoc -q user doesn't show anything relevant, but perldoc -q
uid gives $< ... which gives the numerical id of the user. I need to get
the name from this. Is there a straightforward way of doing this or do I
have to grep /etc/passwd? ... or is there another way?


Justin.
 
J

Justin C

my $name = getpwuid $<

Bingo! Great thanks.

Just to confirm, getpwuid $< returns a list, and the rest of the list is
ignored because I've not asked perl to do anything with it? If I'd used:
my @name = getpwuid $<
instead I'd have had a whole list of stuff, where $name[0] is the name?
I suppose I should really try this for myself...

Thanks for the reply.

Justin.
 
D

David Squire

Justin said:
Bingo! Great thanks.

Just to confirm, getpwuid $< returns a list, and the rest of the list is
ignored because I've not asked perl to do anything with it?

Not quite. What getpwuid returns depends on the context in which it is
called. You called it in scalar context. See perldoc -f getpwuid.

If it had returned a list, in scalar context you would have got the last
element of the list, not the first.
If I'd used:
my @name = getpwuid $<
instead I'd have had a whole list of stuff, where $name[0] is the name?

Yes, because this time it is called in list context. In list context it
returns a list where the name is the first element.
I suppose I should really try this for myself...

Or read the docs...


DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top