use reusable I/O utils and better error handling
[scpubgit/System-Introspector.git] / lib / System / Introspector / Hosts.pm
CommitLineData
645a8f49 1package System::Introspector::Hosts;
2use Moo;
3
a042903a 4use System::Introspector::Util qw(
5 handle_from_file
6 transform_exceptions
7);
8
8d888cc1 9has hosts_file => (
10 is => 'ro',
11 default => sub { '/etc/hosts' },
12);
13
645a8f49 14sub gather {
15 my ($self) = @_;
a042903a 16 return transform_exceptions {
17 my $fh = $self->_open_hosts_file;
18 my @hosts;
19 while (defined( my $line = <$fh> )) {
20 chomp $line;
21 next if $line =~ m{^\s*$}
22 or $line =~ m{^\s*#};
23 push @hosts, [split m{\s+}, $line];
24 }
25 return { hosts => \@hosts };
26 };
645a8f49 27}
28
29sub _open_hosts_file {
30 my ($self) = @_;
a042903a 31 return handle_from_file $self->hosts_file;
645a8f49 32}
33
341;
535e84b6 35
36__END__
37
38=head1 NAME
39
40System::Introspector::Hosts - Gather known hosts
41
42=head1 DESCRIPTION
43
44Reads a C<hosts> file to produce a list of known hosts
45
46=head1 ATTRIBUTES
47
48=head2 hosts_file
49
50The path to the C<hosts> file that should be read. Defaults to C</etc/hosts>.
51
52=head1 SEE ALSO
53
54=over
55
56=item L<System::Introspector>
57
58=back
59
60=cut
61