3 package System::Introspector::Util;
5 use IPC::Run qw( run );
8 use Scalar::Util qw( blessed );
21 package System::Introspector::_Exception;
23 has message => (is => 'ro');
26 sub fail { die System::Introspector::_Exception->new(message => shift) }
27 sub is_report_exception { ref(shift) eq 'System::Introspector::_Exception' }
33 or fail "Unable to read directory $dir: $!";
35 while (defined( my $item = readdir $dh )) {
36 next if -d "$dir/$item";
42 sub transform_exceptions (&) {
44 my $result = eval { $code->() };
46 return { error => $error->message }
47 if is_report_exception $error;
53 sub output_from_command {
54 my ($command, $in) = @_;
57 my ($out, $err) = ('', '');
58 my $ok = run($command, \$in, \$out, \$err);
59 return $out, $err, $ok
61 $command = join ' ', @$command
63 fail "Error running command ($command): $err"
68 sub lines_from_command {
70 my $output = output_from_command $command;
72 return split m{\n}, $output;
75 sub handle_from_command {
81 my $child_pid = open2($out, File::Spec->devnull, $command);
83 waitpid $child_pid, 0;
84 my $content = join '', @lines;
86 open $pipe, '<', \$content;
92 if blessed($err) and $err->isa('System::Introspector::_Exception');
93 fail "Error from command '$command': $err";
98 sub handle_from_file {
100 open my $fh, '<', $file
101 or fail "Unable to read $file: $!";
105 sub output_from_file {
107 my $fh = handle_from_file $file;
110 return do { local $/; <$fh> };