| 1 | package Reply::Plugin::IPerl; | ||
| 2 | $Reply::Plugin::IPerl::VERSION = '0.012'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Reply::Plugin'; | ||
| 7 | |||
| 8 | use Moo; | ||
| 9 | use Capture::Tiny ':all'; | ||
| 10 | |||
| 11 | sub compile { | ||
| 12 | my $self = shift; | ||
| 13 | my ($next, @args) = @_; | ||
| 14 | |||
| 15 | # capture compile errors | ||
| 16 | local $SIG{__WARN__} = sub { push @{$self->{warning}}, @_ }; | ||
| 17 | |||
| 18 | $next->(@args); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub execute { | ||
| 22 | my $self = shift; | ||
| 23 | my ($next, @args) = @_; | ||
| 24 | |||
| 25 | my @results; | ||
| 26 | local $SIG{__WARN__} = sub { push @{$self->{warning}}, @_ }; | ||
| 27 | my ($stdout, $stderr) = capture { | ||
| 28 | @results = $next->(@args); | ||
| 29 | }; | ||
| 30 | $self->{stdout} = $stdout; | ||
| 31 | $self->{stderr} = $stderr; | ||
| 32 | |||
| 33 | @results; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub print_result { | ||
| 37 | my $self = shift; | ||
| 38 | my ($next, @args) = @_; | ||
| 39 | |||
| 40 | $self->{last_output} = capture { | ||
| 41 | $next->(@args); | ||
| 42 | }; | ||
| 43 | |||
| 44 | $self->{last_output}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub mangle_result { | ||
| 48 | my $self = shift; | ||
| 49 | my @result = @_; | ||
| 50 | |||
| 51 | $self->{results} = \@result; | ||
| 52 | |||
| 53 | # to avoid having the problem when result cache plugin is not loaded | ||
| 54 | return if not @result; | ||
| 55 | |||
| 56 | return @result; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub mangle_error { | ||
| 60 | my $self = shift; | ||
| 61 | my $error = shift; | ||
| 62 | |||
| 63 | $self->{error} = $error; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub clear_data { | ||
| 67 | my ($self) = @_; | ||
| 68 | for my $field ( qw(results stdout stderr error warning last_output) ) { | ||
| 69 | $self->{$field} = undef; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub results { my $self = shift; $self->{results}; } | ||
| 74 | sub stdout { my $self = shift; $self->{stdout}; } | ||
| 75 | sub stderr { my $self = shift; $self->{stderr}; } | ||
| 76 | sub error { my $self = shift; $self->{error}; } | ||
| 77 | sub warning { my $self = shift; $self->{warning}; } | ||
| 78 | sub last_output { my $self = shift; $self->{last_output}; } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ |