exception handling and command pipe output utils
Robert 'phaylon' Sedlacek [Wed, 9 May 2012 22:08:17 +0000 (22:08 +0000)]
lib/System/Introspector/Util.pm [new file with mode: 0644]

diff --git a/lib/System/Introspector/Util.pm b/lib/System/Introspector/Util.pm
new file mode 100644 (file)
index 0000000..f9c9d85
--- /dev/null
@@ -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;