use reusable I/O utils and better error handling
Robert 'phaylon' Sedlacek [Thu, 10 May 2012 02:39:11 +0000 (02:39 +0000)]
lib/System/Introspector/ResolvConf.pm

index a33bd97..7794d4e 100644 (file)
@@ -1,6 +1,11 @@
 package System::Introspector::ResolvConf;
 use Moo;
 
+use System::Introspector::Util qw(
+    handle_from_file
+    transform_exceptions
+);
+
 has resolv_conf_file => (
     is      => 'ro',
     default => sub { '/etc/resolv.conf' },
@@ -8,23 +13,22 @@ has resolv_conf_file => (
 
 sub gather {
     my ($self) = @_;
-    my $fh = $self->_open_resolv_conf_file;
-    my @resolv;
-    while (defined( my $line = <$fh> )) {
-        chomp $line;
-        next if $line =~ m{^\s*$}
-             or $line =~ m{^\s*#};
-        push @resolv, [split m{\s+}, $line];
-    }
-    return \@resolv;
+    return transform_exceptions {
+        my $fh = $self->_open_resolv_conf_file;
+        my @resolv;
+        while (defined( my $line = <$fh> )) {
+            chomp $line;
+            next if $line =~ m{^\s*$}
+                 or $line =~ m{^\s*#};
+            push @resolv, [split m{\s+}, $line];
+        }
+        return { resolv_conf => \@resolv };
+    };
 }
 
 sub _open_resolv_conf_file {
     my ($self) = @_;
-    my $file = $self->resolv_conf_file;
-    open my $fh, '<', $file
-        or die "Unable to read $file: $!";
-    return $fh;
+    return handle_from_file $self->resolv_conf_file;
 }
 
 1;