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
8d888cc1 4has hosts_file => (
5 is => 'ro',
6 default => sub { '/etc/hosts' },
7);
8
645a8f49 9sub gather {
10 my ($self) = @_;
11 my $fh = $self->_open_hosts_file;
12 my @hosts;
13 while (defined( my $line = <$fh> )) {
14 chomp $line;
15 next if $line =~ m{^\s*$}
16 or $line =~ m{^\s*#};
17 push @hosts, [split m{\s+}, $line];
18 }
19 return \@hosts;
20}
21
22sub _open_hosts_file {
23 my ($self) = @_;
8d888cc1 24 my $file = $self->hosts_file;
25 open my $fh, '<', $file
26 or die "Unable to read $file: $!\n";
645a8f49 27 return $fh;
28}
29
301;
535e84b6 31
32__END__
33
34=head1 NAME
35
36System::Introspector::Hosts - Gather known hosts
37
38=head1 DESCRIPTION
39
40Reads a C<hosts> file to produce a list of known hosts
41
42=head1 ATTRIBUTES
43
44=head2 hosts_file
45
46The path to the C<hosts> file that should be read. Defaults to C</etc/hosts>.
47
48=head1 SEE ALSO
49
50=over
51
52=item L<System::Introspector>
53
54=back
55
56=cut
57