blib/lib/Devel/Trepan/CmdProcessor/Eval.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 197 CC 24 SCAR 52.6
CC
24
Cov
0%
handle_eval_result
342.0
eval
56.0
BEGIN
2.0
CRAP 193.1
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2012-2014 Rocky Bernstein <rocky@cpan.org>
3 use warnings; use utf8;
4 use rlib '../../..';
5  
6 package Devel::Trepan::CmdProcessor;
7 use Devel::Trepan::Util qw(hash_merge uniq_abbrev);
8 use PadWalker qw(peek_my peek_our);
9 use strict;
10  
11 # Note DB::Eval uses and sets its own variables.
12  
13 use constant HAVE_EVAL_WITH_LEXICALS => eval("use Eval::WithLexicals; 1") ? 1 : 0;
14  
15 my $given_eval_warning = 0;
16  
17 sub eval($$$$) {
18 my ($self, $code_to_eval, $opts) = @_;
19 no warnings 'once';
20 my $return_type = $opts->{return_type};
21 if (0 == $self->{frame_index} || !HAVE_EVAL_WITH_LEXICALS) {
22 unless (0 == $self->{frame_index}) {
23 $self->msg("Evaluation occurs in top-most frame not this one");
24 }
25 $DB::eval_str = $self->{dbgr}->evalcode($code_to_eval);
26 $DB::eval_opts = $opts;
27 $DB::result_opts = $opts;
28  
29 ## This doesn't work because it doesn't pick up "my" variables
30 # DB::eval_with_return($code_to_eval, $opts, @DB::saved);
31 # $self->process_after_eval();
32  
33 # All the way back to DB seems to work here.
34 $self->{DB_running} = 2;
35 $self->{leave_cmd_loop} = 1;
36  
37 } else {
38 # Have to use Eval::WithLexicals which, unfortunately,
39 # loses on 'local' variables.
40  
41 my $stack_size_with_debugger = 0;
42 while (my ($pkg, $file, $line, $fn) =
43 caller($stack_size_with_debugger++)) { ; };
44 my $diff = $stack_size_with_debugger - $self->{stack_size};
45  
46 my $my_hash = peek_my($diff + $self->{frame_index} - 1);
47 my $our_hash = peek_our($diff + $self->{frame_index} - 1);
48 my $var_hash = hash_merge($my_hash, $our_hash);
49  
50 unless ($given_eval_warning) {
51 $self->msg("Evaluation in this frame may not find local values");
52 $given_eval_warning = 0 # 1;
53 }
54  
55 my $context = 'scalar';
56 $return_type = '$' unless defined($return_type);
57 if ('@' eq $return_type) {
58 $context = 'list';
59 $code_to_eval = "\@DB::eval_result = $code_to_eval";
60 } else {
61 ## FIXME do fixup for hash.
62 $context = 'scalar';
63 $code_to_eval = "\$DB::eval_result = $code_to_eval";
64 }
65 my $eval = Eval::WithLexicals->new(
66 lexicals => $var_hash,
67 in_package => $self->{frame}{pkg},
68 context => $context,
69 # prelude => 'use warnings', # default 'use strictures 1'
70 );
71 $eval->eval($code_to_eval);
72 }
73 if ('@' eq $return_type) {
74 return @DB::eval_result;
75 } else {
76 return $DB::eval_result;
77 }
78 }
79  
80 # FIXME: have a way to customize Data::Dumper, PerlTidy etc.
81 require Data::Dumper;
82 # FIXME: remove this when converted to OO forms of Data::Dumper
83 $Data::Dumper::Terse = 1;
84  
85 my $last_eval_value = 0;
86  
87 sub handle_eval_result($) {
88 my ($self) = @_;
89 my $val_str;
90 my $prefix="\$DB::D[$last_eval_value] =";
91  
92 # Perltidy::Dumper uses Tidy which looks at @ARGV for filenames.
93 # Having a non-empty @ARGV will cause Tidy to croak.
94 local @ARGV=();
95  
96 my $fn;
97 my $print_properties = {};
98 my $evdisp = $self->{settings}{displayeval};
99  
100 # FIXME: switch over entirely to the OO way of using Data::Dumper
101 # than set this global.
102 my $old_terse = $Data::Dumper::Terse;
103 $Data::Dumper::Terse = 1;
104  
105  
106 # FIXME: this is way ugly. We could probably use closures
107 # (anonymous subroutines) to combine this and the if code below
108 if ('tidy' eq $evdisp) {
109 $fn = \&Data::Dumper::Perltidy::Dumper;
110 } elsif ('ddp' eq $evdisp) {
111 $print_properties = {
112 colored => $self->{settings}{highlight},
113 };
114 $fn = \&Data::Printer::p;
115 } elsif ('concise' eq $evdisp) {
116 $fn = \&Data::Dumper::Concise::Dumper;
117 } else {
118 $fn = \&Data::Dumper::Dumper;
119 }
120 my $return_type = $DB::eval_opts->{return_type};
121 $return_type = '' unless defined $return_type;
122 if ('$' eq $return_type) {
123 if (defined $DB::eval_result) {
124 $DB::D[$last_eval_value++] = $DB::eval_result;
125 if ('ddp' eq $evdisp) {
126 $val_str =
127 $fn->(\$DB::eval_result, %$print_properties);
128 } else {
129 $val_str = $fn->($DB::eval_result);
130 }
131 chomp $val_str;
132 } else {
133 $DB::eval_result = '<undef>' ;
134 }
135 $self->msg("$prefix $DB::eval_result");
136 } elsif ('@' eq $return_type) {
137 if (@DB::eval_result) {
138 $val_str = $fn->(\@DB::eval_result, %$print_properties);
139 chomp $val_str;
140 @{$DB::D[$last_eval_value++]} = @DB::eval_result;
141 } else {
142 $val_str = '<undef>'
143 }
144 $self->msg("$prefix\n\@\{$val_str}");
145 } elsif ('%' eq $return_type) {
146 if (%DB::eval_result) {
147 if ('dumper' eq $evdisp) {
148 my $d = Data::Dumper->new([\%DB::eval_result]);
149 $d->Terse(1)->Sortkeys(1);
150 $val_str = $d->Dump()
151 } elsif ('ddp' eq $evdisp) {
152 $val_str = $fn->(\%DB::eval_result, %$print_properties);
153 } else {
154 $val_str = $fn->(\%DB::eval_result);
155 }
156 chomp $val_str;
157 @{$DB::D[$last_eval_value++]} = %DB::eval_result;
158 } else {
159 $val_str = '<undef>'
160 }
161 $self->msg("$prefix\n\@\{$val_str}");
162 } elsif ('>' eq $return_type || '2>' eq $return_type ) {
163 $self->msg($DB::eval_result);
164 } else {
165 if (defined $DB::eval_result) {
166 if ('ddp' eq $evdisp) {
167 $val_str = $DB::D[$last_eval_value++] =
168 $fn->(\$DB::eval_result, %$print_properties);
169 } else {
170 $val_str = $DB::D[$last_eval_value++] =
171 $fn->($DB::eval_result);
172 }
173 chomp $val_str;
174 } else {
175 $val_str = '<undef>'
176 }
177 $self->msg("$prefix ${val_str}");
178 }
179  
180 if (defined($self->{set_wp})) {
181 $self->{set_wp}->old_value($DB::eval_result);
182 $self->{set_wp} = undef;
183 }
184  
185 $DB::eval_opts = {
186 return_type => '',
187 };
188 $DB::eval_result = undef;
189 @DB::eval_result = undef;
190  
191 $Data::Dumper::Terse = $old_terse;
192  
193 }
194  
195 unless (caller) {
196 }
197 scalar "Just one part of the larger Devel::Trepan::CmdProcessor";