Hosts probe for /etc/hosts info
Robert 'phaylon' Sedlacek [Thu, 3 May 2012 16:13:23 +0000 (16:13 +0000)]
lib/System/Introspector/Hosts.pm [new file with mode: 0644]

diff --git a/lib/System/Introspector/Hosts.pm b/lib/System/Introspector/Hosts.pm
new file mode 100644 (file)
index 0000000..d5e880c
--- /dev/null
@@ -0,0 +1,24 @@
+package System::Introspector::Hosts;
+use Moo;
+
+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;
+}
+
+sub _open_hosts_file {
+    my ($self) = @_;
+    open my $fh, '<', '/etc/hosts'
+        or die "Unable to read /etc/hosts: $!\n";
+    return $fh;
+}
+
+1;