loris has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Given a login name, what would be the most elegant way of obtaining the corresponding real name?

I thought of reading the piped output of finger and picking the real name out of that, but it seems a rather clunky approach

Thanks,

loris

Replies are listed 'Best First'.
Re: Determining real name from login name
by gellyfish (Monsignor) on Nov 30, 2004 at 14:00 UTC
    perl -e'print +(getpwnam("jonathan"))[6]'
Re: Determining real name from login name
by zejames (Hermit) on Nov 30, 2004 at 13:58 UTC
    You can use Unix::PasswdFile :
    use Unix::PasswdFile; $pw = new Unix::PasswdFile "/etc/passwd"; my $realname = $pw->gecos("toto");

    HTH

    --
    zejames

      And then you move to a system using NIS/YP or LDAP and wonder why your script breaks . . . . getpwuid() and friends work just fine and are core; no need for an extra module.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Determining real name from login name
by Jenda (Abbot) on Nov 30, 2004 at 23:03 UTC

    Erm. Would you care to tell us your OS? This ain't an Operating System Xxx mailing list. Don't asume everyone uses the same OS you do.

    Jenda
    We'd like to help you learn to help yourself
    Look around you, all you see are sympathetic eyes
    Stroll around the grounds until you feel at home
       -- P. Simon in Mrs. Robinson

      This is a good hint, if you try that with Windows and Active Perl 5.6.x you get:

      The getpwnam function is unimplemented at -e line 1.

      And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
      (Terry Pratchett, Small Gods)

      Hello,

      Thanks for the advice. Sorry for not mentioning the OS - it's SunOS 5.8.

      I looked at http://www.perldoc.com/perl5.8.4/pod/func/getpwnam.html, where I found

      ($name,$passwd,$uid,$gid, $quota,$comment,$gcos,$dir,$shell,$expire) = getpw*;

      but I don't understand what the star means and it seemed to cause a syntax error. Can anyone enlighten me here?

      I did though manage to solve my problem with

      my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($loginName);

      Thanks again for the help,

      loris