| 1 | package Padre::Plugin::ParserTool::Dialog; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Params::Util (); | ||
| 7 | use Padre::Plugin::ParserTool::FBP (); | ||
| 8 | |||
| 9 | our $VERSION = '0.01'; | ||
| 10 | our @ISA = 'Padre::Plugin::ParserTool::FBP'; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | WHITE => Wx::Colour->new('#FFFFFF'), | ||
| 14 | GREEN => Wx::Colour->new('#CCFFCC'), | ||
| 15 | RED => Wx::Colour->new('#FFCCCC'), | ||
| 16 | }; | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | ###################################################################### | ||
| 23 | # Padre::Plugin::ParserTool::FPB Methods | ||
| 24 | |||
| 25 | sub refresh { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | # Reset all dialog colours | ||
| 29 | $self->module->SetBackgroundColour(WHITE); | ||
| 30 | $self->function->SetBackgroundColour(WHITE); | ||
| 31 | $self->dumper->SetBackgroundColour(WHITE); | ||
| 32 | $self->output->SetBackgroundColour(WHITE); | ||
| 33 | |||
| 34 | # Check the module | ||
| 35 | my $module = $self->module->GetValue; | ||
| 36 | unless ( Params::Util::_CLASS($module) ) { | ||
| 37 | $self->module->SetBackgroundColour(RED); | ||
| 38 | return $self->fail("Missing or invalid module '$module'"); | ||
| 39 | } | ||
| 40 | |||
| 41 | # Load the module | ||
| 42 | SCOPE: { | ||
| 43 | local $@; | ||
| 44 | eval "require $module"; | ||
| 45 | if ($@) { | ||
| 46 | $self->module->SetBackgroundColour(RED); | ||
| 47 | return $self->fail("Failed to load '$module': $@"); | ||
| 48 | } | ||
| 49 | $self->module->SetBackgroundColour(GREEN); | ||
| 50 | } | ||
| 51 | |||
| 52 | # Call the code | ||
| 53 | local $@; | ||
| 54 | my $code = $self->function->GetValue; | ||
| 55 | my $rv = do { | ||
| 56 | local $_ = $self->input->GetValue; | ||
| 57 | eval $code; | ||
| 58 | }; | ||
| 59 | if ($@) { | ||
| 60 | $self->function->SetBackgroundColour(RED); | ||
| 61 | return $self->fail("Failed to execute '$code': $@"); | ||
| 62 | } | ||
| 63 | $self->function->SetBackgroundColour(GREEN); | ||
| 64 | |||
| 65 | # Serialize the output | ||
| 66 | local $@; | ||
| 67 | my $dumper = $self->{dumper}->GetStringSelection; | ||
| 68 | my $output = ''; | ||
| 69 | my $error = ''; | ||
| 70 | if ( $dumper eq 'Stringify' ) { | ||
| 71 | $output = eval { defined $rv ? "$rv" : 'undef'; }; | ||
| 72 | $error = "Exception during stringification: $@" if $@; | ||
| 73 | |||
| 74 | } elsif ( $dumper eq 'Data::Dumper' ) { | ||
| 75 | eval { | ||
| 76 | require Data::Dumper; | ||
| 77 | $output = Data::Dumper::Dumper($rv); | ||
| 78 | }; | ||
| 79 | $error = "Exception during Data::Dumper: $@" if $@; | ||
| 80 | |||
| 81 | } elsif ( $dumper eq 'Devel::Dumpvar' ) { | ||
| 82 | eval { | ||
| 83 | require Devel::Dumpvar; | ||
| 84 | $output = Devel::Dumpvar->new( | ||
| 85 | to => 'return', | ||
| 86 | )->dump($rv); | ||
| 87 | }; | ||
| 88 | $error = "Exception during Devel::Dumpvar: $@" if $@; | ||
| 89 | |||
| 90 | } elsif ( $dumper eq 'PPI::Dumper' ) { | ||
| 91 | eval { | ||
| 92 | unless ( Params::Util::_INSTANCE( $rv, 'PPI::Element' ) ) | ||
| 93 | { | ||
| 94 | die "Not a PPI::Element object"; | ||
| 95 | } | ||
| 96 | require PPI::Dumper; | ||
| 97 | $output = PPI::Dumper->new($rv)->string; | ||
| 98 | }; | ||
| 99 | $error = "Exception during PPI::Dumper: $@" if $@; | ||
| 100 | |||
| 101 | } else { | ||
| 102 | $error = "Unknown or unsupported dumper '$dumper'"; | ||
| 103 | } | ||
| 104 | if ($error) { | ||
| 105 | $self->dumper->SetBackgroundColour(RED); | ||
| 106 | return $self->fail($error); | ||
| 107 | } | ||
| 108 | |||
| 109 | # Print the output | ||
| 110 | $self->dumper->SetBackgroundColour(GREEN); | ||
| 111 | $self->output->SetValue($output); | ||
| 112 | |||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub fail { | ||
| 117 | my $self = shift; | ||
| 118 | $self->output->SetBackgroundColour(RED); | ||
| 119 | $self->output->SetValue(shift); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; |