3 package System::Introspector::Util;
5 use IPC::Run qw( run );
21 or die "Unable to read directory $dir: $!\n";
23 while (defined( my $item = readdir $dh )) {
24 next if -d "$dir/$item";
30 sub transform_exceptions (&) {
33 my $result = eval { $code->() };
34 return { error => "$@" }
39 sub output_from_command {
40 my ($command, $in) = @_;
43 my ($out, $err) = ('', '');
44 my $ok = run($command, \$in, \$out, \$err);
45 return $out, $err, $ok
47 $command = join ' ', @$command
49 die "Error running command ($command): $err\n"
54 sub lines_from_command {
56 my $output = output_from_command $command;
58 return split m{\n}, $output;
61 sub handle_from_command {
63 open my $pipe, '-|', $command
64 or die "Unable to read from command '$command': $!\n";
68 sub handle_from_file {
70 open my $fh, '<', $file
71 or die "Unable to read $file: $!\n";
75 sub output_from_file {
77 my $fh = handle_from_file $file;
80 return do { local $/; <$fh> };