3 package System::Introspector::Util;
5 use IPC::Run qw( run );
20 or die "Unable to read directory $dir: $!\n";
22 while (defined( my $item = readdir $dh )) {
23 next if -d "$dir/$item";
29 sub transform_exceptions (&) {
32 my $result = eval { $code->() };
33 return { error => "$@" }
38 sub output_from_command {
39 my ($command, $in) = @_;
42 my ($out, $err) = ('', '');
43 my $ok = run($command, \$in, \$out, \$err);
44 return $out, $err, $ok
46 $command = join ' ', @$command
48 die "Error running command ($command): $err\n"
53 sub handle_from_command {
55 open my $pipe, '-|', $command
56 or die "Unable to read from command '$command': $!\n";
60 sub handle_from_file {
62 open my $fh, '<', $file
63 or die "Unable to read $file: $!\n";
67 sub output_from_file {
69 my $fh = handle_from_file $file;
72 return do { local $/; <$fh> };