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