6 use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
7 @EXPORT = qw(getpwent getpwuid getpwnam getpw);
9 $pw_name $pw_passwd $pw_uid
10 $pw_gid $pw_quota $pw_comment
11 $pw_gecos $pw_dir $pw_shell
13 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
17 # Class::Struct forbids use of @ISA
18 sub import { goto &Exporter::import }
20 use Class::Struct qw(struct);
21 struct 'User::pwent' => [
37 ( $pw_name, $pw_passwd, $pw_uid,
38 $pw_gid, $pw_quota, $pw_comment,
39 $pw_gecos, $pw_dir, $pw_shell, ) = @$pwob = @_;
44 sub getpwent ( ) { populate(CORE::getpwent()) }
45 sub getpwnam ($) { populate(CORE::getpwnam(shift)) }
46 sub getpwuid ($) { populate(CORE::getpwuid(shift)) }
47 sub getpw ($) { ($_[0] =~ /^\d+/) ? &getpwuid : &getpwnam }
54 User::pwent - by-name interface to Perl's built-in getpw*() functions
59 $pw = getpwnam('daemon') or die "No daemon user";
60 if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?$# ) {
61 print "gid 1 on root dir";
64 use User::pwent qw(:FIELDS);
65 getpwnam('daemon') or die "No daemon user";
66 if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?$# ) {
67 print "gid 1 on root dir";
70 $pw = getpw($whoever);
74 This module's default exports override the core getpwent(), getpwuid(),
75 and getpwnam() functions, replacing them with versions that return
76 "User::pwent" objects. This object has methods that return the similarly
77 named structure field name from the C's passwd structure from F<pwd.h>;
78 namely name, passwd, uid, gid, quota, comment, gecos, dir, and shell.
80 You may also import all the structure fields directly into your namespace
81 as regular variables using the :FIELDS import tag. (Note that this still
82 overrides your core functions.) Access these fields as
83 variables named with a preceding C<pw_> in front their method names.
84 Thus, C<$passwd_obj-E<gt>shell()> corresponds to $pw_shell if you import
87 The getpw() function is a simple front-end that forwards
88 a numeric argument to getpwuid() and the rest to getpwnam().
90 To access this functionality without the core overrides,
91 pass the C<use> an empty import list, and then access
92 function functions with their full qualified names.
93 On the other hand, the built-ins are still available
94 via the C<CORE::> pseudo-package.
98 While this class is currently implemented using the Class::Struct
99 module to build a struct-like class, you shouldn't rely upon this.