From: Robert 'phaylon' Sedlacek Date: Thu, 10 May 2012 02:28:14 +0000 (+0000) Subject: use reusable I/O utils and better error handling X-Git-Tag: v0.001_001~87 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a042903a15cc47819ee390c59b584f8cde7b4677;p=scpubgit%2FSystem-Introspector.git use reusable I/O utils and better error handling --- diff --git a/lib/System/Introspector/Hosts.pm b/lib/System/Introspector/Hosts.pm index ed8a1bf..5f01f37 100644 --- a/lib/System/Introspector/Hosts.pm +++ b/lib/System/Introspector/Hosts.pm @@ -1,6 +1,11 @@ package System::Introspector::Hosts; use Moo; +use System::Introspector::Util qw( + handle_from_file + transform_exceptions +); + has hosts_file => ( is => 'ro', default => sub { '/etc/hosts' }, @@ -8,23 +13,22 @@ has hosts_file => ( sub gather { my ($self) = @_; - my $fh = $self->_open_hosts_file; - my @hosts; - while (defined( my $line = <$fh> )) { - chomp $line; - next if $line =~ m{^\s*$} - or $line =~ m{^\s*#}; - push @hosts, [split m{\s+}, $line]; - } - return \@hosts; + return transform_exceptions { + my $fh = $self->_open_hosts_file; + my @hosts; + while (defined( my $line = <$fh> )) { + chomp $line; + next if $line =~ m{^\s*$} + or $line =~ m{^\s*#}; + push @hosts, [split m{\s+}, $line]; + } + return { hosts => \@hosts }; + }; } sub _open_hosts_file { my ($self) = @_; - my $file = $self->hosts_file; - open my $fh, '<', $file - or die "Unable to read $file: $!\n"; - return $fh; + return handle_from_file $self->hosts_file; } 1;