use reusable I/O utils and better error handling
[scpubgit/System-Introspector.git] / lib / System / Introspector / Groups.pm
CommitLineData
d4e5a3b2 1package System::Introspector::Groups;
2use Moo;
3
4sub gather {
5 my ($self) = @_;
6 my %group;
7 my $fh = $self->_open_group_file;
8 while (defined( my $line = <$fh> )) {
9 chomp $line;
10 my ($name, undef, $gid, $users) = split m{:}, $line;
11 $users = length($users)
12 ? [split m{,}, $users]
13 : [];
14 $group{ $gid } = {
15 name => $name,
16 gid => $gid,
17 users => $users,
18 };
19 }
20 return \%group;
21}
22
23sub _open_group_file {
24 my ($self) = @_;
25 open my $fh, '<', '/etc/group'
26 or die "Unable to read group file /etc/group: $!\n";
27 return $fh;
28}
29
301;
535e84b6 31
32__END__
33
34=head1 NAME
35
36System::Introspector::Groups - Gather group information
37
38=head1 DESCRIPTION
39
40Uses C</etc/group> to gather information about groups.
41
42=head1 SEE ALSO
43
44=over
45
46=item L<System::Introspector>
47
48=back
49
50=cut
51