From: Robert 'phaylon' Sedlacek Date: Wed, 9 May 2012 22:08:17 +0000 (+0000) Subject: exception handling and command pipe output utils X-Git-Tag: v0.001_001~103 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f79a227c58f129ef98f62220ef7ee5d1f56adb8b;p=scpubgit%2FSystem-Introspector.git exception handling and command pipe output utils --- diff --git a/lib/System/Introspector/Util.pm b/lib/System/Introspector/Util.pm new file mode 100644 index 0000000..f9c9d85 --- /dev/null +++ b/lib/System/Introspector/Util.pm @@ -0,0 +1,27 @@ +use strictures 1; + +package System::Introspector::Util; +use Exporter 'import'; + +our @EXPORT_OK = qw( + handle_from_command + transform_exceptions +); + +sub transform_exceptions (&) { + my ($code) = @_; + local $@; + my $result = eval { $code->() }; + return { error => "$@" } + if $@; + return $result; +} + +sub handle_from_command { + my ($command) = @_; + open my $pipe, '-|', $command + or die "Unable to read from command '$command': $!\n"; + return $pipe; +} + +1;