| 1 | package CGI::Inspect::Plugin::REPL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'CGI::Inspect::Plugin'; | ||
| 5 | use Devel::StackTrace::WithLexicals; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | my $self = $class->SUPER::new(@_); | ||
| 11 | $self->{trace} = Devel::StackTrace::WithLexicals->new; | ||
| 12 | return $self; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub process { | ||
| 16 | my $self = shift; | ||
| 17 | my $cmd = $self->param('cmd'); | ||
| 18 | my $output = $self->{output} || ''; | ||
| 19 | |||
| 20 | my $trace = $self->{trace}; | ||
| 21 | $trace->reset_pointer; | ||
| 22 | my $frame; | ||
| 23 | while($frame = $trace->next_frame) { | ||
| 24 | last if $frame->package() !~ /^(Continuity|Coro|CGI::Inspect)/; | ||
| 25 | } | ||
| 26 | use Data::Dumper; | ||
| 27 | |||
| 28 | my $package = $frame->package; | ||
| 29 | |||
| 30 | my $declarations = join "\n", | ||
| 31 | map {"my $_;"} | ||
| 32 | keys %{ $frame->lexicals || {} }; | ||
| 33 | |||
| 34 | my $aliases = q| | ||
| 35 | while (my ($k, $v) = each %{ $frame->lexicals }) { | ||
| 36 | Devel::LexAlias::lexalias 0, $k, $v; | ||
| 37 | } | ||
| 38 | |; | ||
| 39 | |||
| 40 | my $code = qq| | ||
| 41 | package $package; | ||
| 42 | use Devel::LexAlias; | ||
| 43 | no warnings 'misc'; # declaration in same scope masks earlier instance | ||
| 44 | no strict 'vars'; # so we get all the global variables in our package | ||
| 45 | $declarations | ||
| 46 | sub __execute_cmd { | ||
| 47 | $aliases | ||
| 48 | return $cmd | ||
| 49 | } | ||
| 50 | return __execute_cmd(); | ||
| 51 | |; | ||
| 52 | |||
| 53 | if($cmd) { | ||
| 54 | local $Data::Dumper::Terse = 1; | ||
| 55 | my $new_output = "\n> " . $cmd . "\n"; | ||
| 56 | $new_output .= Dumper(eval($code)); | ||
| 57 | $new_output .= "Error: $@\n" if $@; | ||
| 58 | $new_output =~ s{<}{\<}g; | ||
| 59 | $output .= $new_output; | ||
| 60 | } | ||
| 61 | $self->{output} = $output; | ||
| 62 | |||
| 63 | $output = qq{ | ||
| 64 | <div class=dialog id=repl title='Read-Eval-Print'> | ||
| 65 | <pre> | ||
| 66 | $output | ||
| 67 | </pre> | ||
| 68 | > <input type=text name=cmd id=cmd size=40> | ||
| 69 | <input type=submit name=send value="Send"></pre> | ||
| 70 | <script>document.getElementById('cmd').focus()</script> | ||
| 71 | </div> | ||
| 72 | }; | ||
| 73 | |||
| 74 | return $output; | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 |