User::pwent(3pm)
π User::pwent β bestie edition π
ok so this is a page from the Perl Programmers Reference Guide (yes, the whole User::pwent(3pm) situation), and honestly? it's giving "make getpw*() actually cute to use." let's get into it π
β¨ NAME β¨
User::pwent β a by-name interface to Perl's built-in getpw*() functions. that's it, that's the vibe π«Ά
π SYNOPSIS π
ok so here's how you actually use this thing, no cap:
use User::pwent;
my $pw = getpwnam('daemon') || die "No daemon user";
if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?\z#s ) {
print "gid 1 on root dir";
}
my $real_shell = $pw->shell || '/bin/sh';
for (my ($fullname, $office, $workphone, $homephone) =
split /\s*,\s*/, $pw->gecos)
{
s/&/ucfirst(lc($pw->name))/ge;
}
use User::pwent qw(:FIELDS);
getpwnam('daemon') || die "No daemon user";
if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?\z#s ) {
print "gid 1 on root dir";
}
my $pw = getpw($whoever);
use User::pwent qw/:DEFAULT pw_has/;
if (pw_has(qw[gecos expire quota])) { .... }
if (pw_has("name uid gid passwd")) { .... }
print "Your struct pwd has: ", scalar pw_has(), "\n";
like β look at it. pw_has checking fields, $pw->gecos doing its little & substitution dance. iconic. π
πΈ DESCRIPTION πΈ
okay so the tea β: this module's default exports override the core getpwent(), getpwuid(), and getpwnam() functions, swapping them out for versions that return "User::pwent" objects. lowkey genius.
this object has methods that return the similarly named structure field name from C's passwd structure from pwd.h, stripped of their leading "pw_" parts. the whole list, and yes you're getting all of them:
namepasswduidgidchangeagequotacommentclassgecosdirshellexpire
β οΈ heads up bestie: thepasswd,gecos, andshellfields are tainted when running in taint mode. that's just how it is π«
you may also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (note that this still overrides your core functions, fyi.) access these fields as variables named with a preceding "pw_" in front of their method names. thus, $passwd_obj->shell corresponds to $pw_shell if you import the fields. the way that mapping just works?? π¦
the getpw() function is a simple front-end β it forwards a numeric argument to getpwuid() and the rest to getpwnam(). short king behavior honestly π
and if you want to access this functionality without the core overrides? just pass the "use" an empty import list, then access function functions with their full qualified names. the built-ins are always still available via the "CORE::" pseudo-package. she's flexible like that πββοΈ
π§ System Specifics π§
Perl believes that no machine ever has more than one of change, age, or quota implemented, nor more than one of either comment or class. some machines do not support expire, gecos, or allegedly, passwd. π
you may call these methods no matter what machine you're on β but they return "undef" if unimplemented. so like, no error, just vibes and undef. π
you may ask whether one of these was implemented on the system Perl was built on by asking the importable pw_has function about them. here's the deal:
- returns true if all parameters are supported fields on the build platform
- returns false if one or more were not
- raises an exception if you asked about a field that Perl never knows how to provide π (don't do that)
parameters may be in a space-separated string, or as separate arguments. and if you pass no parameters? the function returns the list of struct pwd fields supported by your build platform's C library β as a list in list context, or a space-separated string in scalar context. β
π«Ά note: just because your C library had a field doesn't necessarily mean that it's fully implemented on that system. plot twist.
interpretation of the gecos field varies between systems, but traditionally it holds 4 comma-separated fields containing the user's full name, office location, work phone number, and home phone number. an "&" in the gecos field should be replaced by the user's properly capitalized login "name". π
the shell field, if blank, must be assumed to be /bin/sh. Perl does not do this for you. (that's literally why the synopsis has my $real_shell = $pw->shell || '/bin/sh'; β she's showing you the way π)
the passwd is one-way hashed garble, not clear text, and may not be unhashed save by brute-force guessing. secure systems use more a more secure hashing than DES. π₯
and on systems supporting shadow password systems, Perl automatically returns the shadow password entry when called by a suitably empowered user β even if your underlying vendor-provided C library was too short-sighted to realize it should do this. Perl said "I'll do it myself" π
see passwd(5) and getpwent(3) for details.
π NOTE π
while this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this. it's giving internal implementation detail. don't get attached. π©
π AUTHOR π
Tom Christiansen. legend behavior, fr fr. π«‘
β HISTORY β
March 18th, 2000 Reworked internals to support better interface to dodgey fields than normal Perl function provides. Added pw_has() field. Improved documentation. a glow-up, honestly β¨
and that's the whole page bestie!! π this one's from perl v5.42.3, dated 2026-11-03, and it's the User::pwent(3pm) manpage β go forth and getpwnam responsibly πΈπ¦π
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms