4f74d2da0ed2a06c5b996ff19ef7a09ae493b2fd
[scpubgit/System-Introspector.git] / lib / System / Introspector / Hosts.pm
1 package System::Introspector::Hosts;
2 use Moo;
3
4 has hosts_file => (
5     is      => 'ro',
6     default => sub { '/etc/hosts' },
7 );
8
9 sub 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
22 sub _open_hosts_file {
23     my ($self) = @_;
24     my $file = $self->hosts_file;
25     open my $fh, '<', $file
26         or die "Unable to read $file: $!\n";
27     return $fh;
28 }
29
30 1;