Commit | Line | Data |
36cf3bcb |
1 | package Tak::EvalService; |
2 | |
3 | use Eval::WithLexicals; |
4 | use Moo; |
5 | use Data::Dumper::Concise; |
6 | use Capture::Tiny qw(capture); |
7 | |
8 | has 'eval_withlexicals' => (is => 'lazy'); |
9 | |
10 | sub _build_eval_withlexicals { |
11 | Eval::WithLexicals->new |
12 | } |
13 | |
14 | sub 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 | |
44 | 1; |