more explicit ipc to work around child-exec issues
[scpubgit/System-Introspector.git] / lib / System / Introspector / Util.pm
index d699799..98d2429 100644 (file)
@@ -3,6 +3,9 @@ use strictures 1;
 package System::Introspector::Util;
 use Exporter 'import';
 use IPC::Run qw( run );
+use IPC::Open2;
+use File::Spec;
+use Scalar::Util qw( blessed );
 
 our @EXPORT_OK = qw(
     handle_from_command
@@ -71,8 +74,24 @@ sub lines_from_command {
 
 sub handle_from_command {
     my ($command) = @_;
-    open my $pipe, '-|', $command
-        or fail "Unable to read from command '$command': $!";
+    my $pipe;
+    local $@;
+    my $ok = eval {
+        my $out;
+        my $child_pid = open2($out, File::Spec->devnull, $command);
+        my @lines = <$out>;
+        waitpid $child_pid, 0;
+        my $content = join '', @lines;
+        my $status = $? >> 8;
+        open $pipe, '<', \$content;
+        1;
+    };
+    unless ($ok) {
+        my $err = $@;
+        die $err
+            if blessed($err) and $err->isa('System::Introspector::_Exception');
+        fail "Error from command '$command': $err";
+    }
     return $pipe;
 }