I can see only two reasons for manually parsing /etc/passwd:

To learn handling CSV files, there are much better ways. And even then, Text::CSV and Text::CSV_XS are the better way to handle CSV files, simply because those modules are prepared to handle all the nasty edge cases you didn't think about.

Verifying a username and/or home directory does not seem like "very special needs".

You probably just want to use getpwnam in list context, i.e.:

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

User::pwent wraps that into a nice object:

use User::pwent; # <-- replace getpwnam() and friends with functions r +eturning objects my $rec = getpwnam($login); say $rec->name,' has UID ',$rec->uid,', home ',$rec->dir,', shell ',$r +ec->shell;

What's the difference to reading /etc/passwd?

The second point is important: /etc/passwd is no longer the only source of user information, since at least three decades. NSS allows to add more records from other sources (NIS, NIS+, LDAP, AD, ...), and it should also allow to ignore /etc/passwd completely. PAM allows to do even more crazy stuff.

The same applies to /etc/group and the getgr... functions, and also to /etc/services (getserv...), /etc/hosts (gethost...), /etc/protocols (getproto...).

At work, all user and group information resides in a Samba 4 Active Directroy database (essentialy LDAP plus some extras), and the /etc/passwd on all Linux systems is at the bare minimum, just root and some system/daemon accounts. If you had an AD account, you could login into any Linux box, but you would not find your login in /etc/passwd. getpwnam works fine, and would return your user record, because at the C library level, LDAP would be queried and return your user record. Plus, it would continue to do so if we switched from AD to NIS+ or some fancy SQL database holding user records.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: Trying to take user input as username, read etc/passwd and output user ID by afoken
in thread Trying to take user input as username, read etc/passwd and output user ID by charlesx1552

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.