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' },
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;