1User::pwent(3pm) Perl Programmers Reference Guide User::pwent(3pm)
2
3
4
6 User::pwent - by-name interface to Perl's built-in getpw*() functions
7
9 use User::pwent;
10 my $pw = getpwnam('daemon') || die "No daemon user";
11 if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?\z#s ) {
12 print "gid 1 on root dir";
13 }
14
15 my $real_shell = $pw->shell || '/bin/sh';
16
17 for (my ($fullname, $office, $workphone, $homephone) =
18 split /\s*,\s*/, $pw->gecos)
19 {
20 s/&/ucfirst(lc($pw->name))/ge;
21 }
22
23 use User::pwent qw(:FIELDS);
24 getpwnam('daemon') || die "No daemon user";
25 if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?\z#s ) {
26 print "gid 1 on root dir";
27 }
28
29 my $pw = getpw($whoever);
30
31 use User::pwent qw/:DEFAULT pw_has/;
32 if (pw_has(qw[gecos expire quota])) { .... }
33 if (pw_has("name uid gid passwd")) { .... }
34 print "Your struct pwd has: ", scalar pw_has(), "\n";
35
37 This module's default exports override the core getpwent(), getpwuid(),
38 and getpwnam() functions, replacing them with versions that return
39 "User::pwent" objects. This object has methods that return the
40 similarly named structure field name from the C's passwd structure from
41 pwd.h, stripped of their leading "pw_" parts, namely "name", "passwd",
42 "uid", "gid", "change", "age", "quota", "comment", "class", "gecos",
43 "dir", "shell", and "expire". The "passwd", "gecos", and "shell"
44 fields are tainted when running in taint mode.
45
46 You may also import all the structure fields directly into your
47 namespace as regular variables using the :FIELDS import tag. (Note
48 that this still overrides your core functions.) Access these fields as
49 variables named with a preceding "pw_" in front their method names.
50 Thus, "$passwd_obj->shell" corresponds to $pw_shell if you import the
51 fields.
52
53 The getpw() function is a simple front-end that forwards a numeric
54 argument to getpwuid() and the rest to getpwnam().
55
56 To access this functionality without the core overrides, pass the "use"
57 an empty import list, and then access function functions with their
58 full qualified names. The built-ins are always still available via the
59 "CORE::" pseudo-package.
60
61 System Specifics
62 Perl believes that no machine ever has more than one of "change",
63 "age", or "quota" implemented, nor more than one of either "comment" or
64 "class". Some machines do not support "expire", "gecos", or allegedly,
65 "passwd". You may call these methods no matter what machine you're on,
66 but they return "undef" if unimplemented.
67
68 You may ask whether one of these was implemented on the system Perl was
69 built on by asking the importable "pw_has" function about them. This
70 function returns true if all parameters are supported fields on the
71 build platform, false if one or more were not, and raises an exception
72 if you asked about a field that Perl never knows how to provide.
73 Parameters may be in a space-separated string, or as separate
74 arguments. If you pass no parameters, the function returns the list of
75 "struct pwd" fields supported by your build platform's C library, as a
76 list in list context, or a space-separated string in scalar context.
77 Note that just because your C library had a field doesn't necessarily
78 mean that it's fully implemented on that system.
79
80 Interpretation of the "gecos" field varies between systems, but
81 traditionally holds 4 comma-separated fields containing the user's full
82 name, office location, work phone number, and home phone number. An
83 "&" in the gecos field should be replaced by the user's properly
84 capitalized login "name". The "shell" field, if blank, must be assumed
85 to be /bin/sh. Perl does not do this for you. The "passwd" is one-way
86 hashed garble, not clear text, and may not be unhashed save by brute-
87 force guessing. Secure systems use more a more secure hashing than
88 DES. On systems supporting shadow password systems, Perl automatically
89 returns the shadow password entry when called by a suitably empowered
90 user, even if your underlying vendor-provided C library was too short-
91 sighted to realize it should do this.
92
93 See passwd(5) and getpwent(3) for details.
94
96 While this class is currently implemented using the Class::Struct
97 module to build a struct-like class, you shouldn't rely upon this.
98
100 Tom Christiansen
101
103 March 18th, 2000
104 Reworked internals to support better interface to dodgey fields
105 than normal Perl function provides. Added pw_has() field.
106 Improved documentation.
107
108
109
110perl v5.38.2 2023-11-30 User::pwent(3pm)