3 package System::Introspector::Util;
5 use IPC::Run qw( run );
18 package System::Introspection::_Exception;
20 has message => (is => 'ro');
23 sub fail { die System::Introspection::_Exception->new(message => shift) }
24 sub is_report_exception { ref(shift) eq 'System::Introspection::_Exception' }
30 or fail "Unable to read directory $dir: $!";
32 while (defined( my $item = readdir $dh )) {
33 next if -d "$dir/$item";
39 sub transform_exceptions (&) {
41 my $result = eval { $code->() };
43 return { error => $error->message }
44 if is_report_exception $error;
50 sub output_from_command {
51 my ($command, $in) = @_;
54 my ($out, $err) = ('', '');
55 my $ok = run($command, \$in, \$out, \$err);
56 return $out, $err, $ok
58 $command = join ' ', @$command
60 fail "Error running command ($command): $err"
65 sub lines_from_command {
67 my $output = output_from_command $command;
69 return split m{\n}, $output;
72 sub handle_from_command {
74 open my $pipe, '-|', $command
75 or fail "Unable to read from command '$command': $!";
79 sub handle_from_file {
81 open my $fh, '<', $file
82 or fail "Unable to read $file: $!";
86 sub output_from_file {
88 my $fh = handle_from_file $file;
91 return do { local $/; <$fh> };