exception handling and command pipe output utils
[scpubgit/System-Introspector.git] / lib / System / Introspector / Util.pm
1 use strictures 1;
2
3 package System::Introspector::Util;
4 use Exporter 'import';
5
6 our @EXPORT_OK = qw(
7     handle_from_command
8     transform_exceptions
9 );
10
11 sub transform_exceptions (&) {
12     my ($code) = @_;
13     local $@;
14     my $result = eval { $code->() };
15     return { error => "$@" }
16         if $@;
17     return $result;
18 }
19
20 sub handle_from_command {
21     my ($command) = @_;
22     open my $pipe, '-|', $command
23         or die "Unable to read from command '$command': $!\n";
24     return $pipe;
25 }
26
27 1;