client and server manage to talk
[scpubgit/Tak.git] / lib / Tak / EvalService.pm
CommitLineData
36cf3bcb 1package Tak::EvalService;
2
3use Eval::WithLexicals;
4use Moo;
5use Data::Dumper::Concise;
6use Capture::Tiny qw(capture);
7
8has 'eval_withlexicals' => (is => 'lazy');
9
10sub _build_eval_withlexicals {
11 Eval::WithLexicals->new
12}
13
14sub handle_eval {
15 my ($self, $perl) = @_;
16 unless ($perl) {
17 return MISTAKE => eval_input => "No code supplied";
18 }
19 if (my $ref = ref($perl)) {
20 return MISTAKE => eval_input => "Code was a ${ref} reference";
21 }
22 my ($code, @ret);
23 my ($stdout, $stderr);
24 if (eval {
25 ($stdout, $stderr) = capture {
26 @ret = $self->eval_withlexicals->eval($perl);
27 };
28 1
29 }) {
30 $code = 'RESULT';
31 } else {
32 ($code, @ret) = (FAILURE => $@);
33 }
34 my $dumped_ret;
35 unless (eval { $dumped_ret = Dumper(@ret); 1 }) {
36 $dumped_ret = "Error dumping ${code} result: $@";
37 $code = 'FAILURE';
38 }
39 return $code => {
40 stdout => $stdout, stderr => $stderr, return => $dumped_ret
41 };
42}
43
441;