From: Robert 'phaylon' Sedlacek Date: Thu, 10 May 2012 02:19:49 +0000 (+0000) Subject: use reusable I/O utils and better error handling X-Git-Tag: v0.001_001~89 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=416de713cf5c44a7a61b6eb48a9ecdf7d858f7c4;p=scpubgit%2FSystem-Introspector.git use reusable I/O utils and better error handling --- diff --git a/lib/System/Introspector/Groups.pm b/lib/System/Introspector/Groups.pm index 8b93e32..15ea3b7 100644 --- a/lib/System/Introspector/Groups.pm +++ b/lib/System/Introspector/Groups.pm @@ -1,30 +1,35 @@ package System::Introspector::Groups; use Moo; +use System::Introspector::Util qw( + handle_from_file + transform_exceptions +); + sub gather { my ($self) = @_; - my %group; - my $fh = $self->_open_group_file; - while (defined( my $line = <$fh> )) { - chomp $line; - my ($name, undef, $gid, $users) = split m{:}, $line; - $users = length($users) - ? [split m{,}, $users] - : []; - $group{ $gid } = { - name => $name, - gid => $gid, - users => $users, - }; - } - return \%group; + return transform_exceptions { + my %group; + my $fh = $self->_open_group_file; + while (defined( my $line = <$fh> )) { + chomp $line; + my ($name, undef, $gid, $users) = split m{:}, $line; + $users = length($users) + ? [split m{,}, $users] + : []; + $group{ $gid } = { + name => $name, + gid => $gid, + users => $users, + }; + } + return { groups => \%group }; + }; } sub _open_group_file { my ($self) = @_; - open my $fh, '<', '/etc/group' - or die "Unable to read group file /etc/group: $!\n"; - return $fh; + return handle_from_file '/etc/group'; } 1;