blib/lib/Devel/Trepan/CmdProcessor/Command/Info_Subcmd/Variables_Subcmd/My.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 211 CC 13 SCAR 27.4
CC
13
Cov
0%
process_args
42.0
complete
20.0
show_var
20.0
CRAP 15.5
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2014 Rocky Bernstein <rocky@cpan.org>
3 use warnings; no warnings 'redefine'; no warnings 'once';
4 use rlib '../../../../../..';
5 use Data::Dumper;
6 $Data::Dumper::Sortkeys = 1;
7  
8 package Devel::Trepan::CmdProcessor::Command::Info::Variables::My;
9 use vars qw(@ISA @SUBCMD_VARS);
10 unless (@ISA) {
11 eval <<'EOE';
12 use constant MAX_ARGS => undef;
13 use constant NEED_STACK => 1;
14 EOE
15 }
16 use strict;
17  
18 use Devel::Trepan::CmdProcessor::Command::Subcmd::Subsubcmd;
19 use PadWalker qw(peek_my);
20 use Devel::Trepan::CmdProcessor::Command::Subcmd::Core;
21  
22 our $CMD = "info variables my";
23 my @CMD = split(/ /, $CMD);
24 =pod
25  
26 =head2 Synopsis:
27  
28 =cut
29 our $MIN_ABBREV = length('m');
30 our $HELP = <<'HELP';
31 =pod
32  
33 B<info variables my>
34  
35 B<info variables my -v>
36  
37 B<info variables my> I<var1> [I<var2>...]
38  
39 Lists C<my> variables at the current frame. Use the frame changing
40 commands like C<up>, C<down> or C<frame> set the current frame.
41  
42 In the first form, give a list of C<my> variable names only. In the
43 second form, list variable names and values In the third form, list
44 variable names and values of I<var1>, etc.
45  
46 =head2 See also:
47  
48 L<C<info variables
49 lexicals>|Devel::Trepan::CmdProcessor::Command::Info::Variables::Lexicals>,
50 L<C<info variables
51 our>|Devel::Trepan::CmdProcessor::Command::Info::Variables::Our>, and
52 frame-changing commands
53  
54 =cut
55 HELP
56 our $SHORT_HELP = "Information about 'my' variables.";
57  
58 @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subsubcmd);
59  
60 sub get_var_hash($;$)
61 {
62 my ($self, $fixup_num) = @_;
63 # FIXME: combine with My.pm
64 my $i = 0;
65 while (my ($pkg, $file, $line, $fn) = caller($i++)) { ; };
66 my $diff = $i - $DB::stack_depth;
67  
68 # FIXME: 5 is a magic fixup constant, also found in DB::finish.
69 # Remove it.
70 $fixup_num = 5 unless defined($fixup_num);
71 my $ref = peek_my($diff + $self->{proc}{frame_index} + $fixup_num);
72 return $ref;
73 }
74  
75 sub complete($$;$)
76 {
77 my ($self, $prefix, $fixup_num) = @_;
78  
79 # This is really hacky
80 unless ($fixup_num) {
81 my $i = 0;
82 while (my ($pkg, $file, $line, $fn) = caller($i++)) {
83 last if $pkg eq 'Devel::Trepan::CmdProcessor' && $fn eq '(eval)';
84 last if $pkg eq 'Devel::Trepan::Core' &&
85 $fn eq 'Devel::Trepan::CmdProcessor::process_commands';
86 };
87  
88 $fixup_num = $i;
89 }
90  
91 # print "FIXUP_NUM is $fixup_num\n";
92  
93 my $var_hash = $self->get_var_hash($fixup_num);
94 my @vars = sort keys %$var_hash;
95 my @results = Devel::Trepan::Complete::complete_token(\@vars, $prefix);
96 return @results;
97 }
98  
99  
100 sub show_var($$$)
101 {
102 my ($proc, $var_name, $ref) = @_;
103 my $dumper;
104 my $type = substr($var_name, 0, 1);
105 if ('$' eq $type) {
106 $dumper = Data::Dumper->new([${$ref}]);
107 $dumper->Useqq(0);
108 $dumper->Terse(1);
109 $dumper->Indent(0);
110 $proc->msg("$var_name = ". $dumper->Dump);
111 } elsif ('@' eq $type) {
112 $dumper = Data::Dumper->new([$ref]);
113 $dumper->Useqq(0);
114 $dumper->Terse(1);
115 $dumper->Indent(0);
116 $proc->msg("$var_name = ". $dumper->Dump);
117 } elsif ('%' eq $type) {
118 $dumper = Data::Dumper->new([$ref], [$var_name]);
119 $dumper->Useqq(0);
120 $dumper->Terse(0);
121 $dumper->Indent(0);
122 $proc->msg($dumper->Dump);
123 } else {
124 $dumper = Data::Dumper->new([$ref], [$var_name]);
125 $dumper->Useqq(0);
126 $dumper->Terse(1);
127 $dumper->Indent(0);
128 $proc->msg($dumper->Dump);
129 };
130 }
131  
132  
133 sub process_args($$$) {
134 my ($self, $args, $hash_ref) = @_;
135 my $lex_type = $self->{prefix}[-1];
136 my $proc = $self->{proc};
137 my @ARGS = @{$args};
138 my @names = sort keys %{$hash_ref};
139  
140 if (0 == scalar @ARGS) {
141 if (scalar @names) {
142 $proc->section("$lex_type variables");
143 $proc->msg($self->{parent}{parent}->columnize_commands(\@names));
144 } else {
145 $proc->msg("No '$lex_type' variables at this level");
146 }
147 } else {
148 if ($ARGS[0] eq '-v') {
149 if (scalar @names) {
150 $proc->section("$lex_type variables");
151 for my $name (@names) {
152 show_var($proc, $name, $hash_ref->{$name});
153 }
154 } else {
155 $proc->msg("No '$lex_type' variables at this level");
156 }
157 } else {
158 for my $name (@ARGS) {
159 if (exists($hash_ref->{$name})) {
160 show_var($proc, $name, $hash_ref->{$name});
161 } else {
162 $proc->errmsg("No '$lex_type' variable $name found at this level");
163 }
164 }
165 }
166 }
167 }
168  
169 sub run($$;$)
170 {
171 my ($self, $args, $fixup_num) = @_;
172 my $var_hash = $self->get_var_hash($fixup_num);
173 my @ARGS = splice(@{$args}, scalar(@CMD));
174 $self->process_args(\@ARGS, $var_hash);
175 }
176  
177 unless (caller) {
178 # Demo it.
179 require Devel::Trepan;
180 my $proc = Devel::Trepan::CmdProcessor->new;
181 my $grandparent =
182 Devel::Trepan::CmdProcessor::Command::Info->new($proc, 'info');
183 my $parent =
184 Devel::Trepan::CmdProcessor::Command::Info::Variables->new($grandparent,
185 'variables');
186 my $cmd = __PACKAGE__->new($parent, 'my');
187  
188 eval {
189 sub create_frame() {
190 my ($pkg, $file, $line, $fn) = caller(0);
191 $DB::package = $pkg;
192 return [
193 {
194 file => $file,
195 fn => $fn,
196 line => $line,
197 pkg => $pkg,
198 }];
199 }
200 };
201 my $frame_ary = create_frame();
202 $proc->frame_setup($frame_ary);
203  
204 $cmd->run($cmd->{prefix}, -2);
205 my @args = @{$cmd->{prefix}};
206 push @args, '$args';
207 print '-' x 40, "\n";
208 $cmd->run(\@args, -2);
209 print '-' x 40, "\n";
210 $cmd->run($cmd->{prefix}, -1);
211 print '-' x 40, "\n";
212 my @complete = $cmd->complete('', -2);
213 print join(', ', @complete), "\n";
214 print '-' x 40, "\n";
215 @complete = $cmd->complete('$p', -2);
216 print join(', ', @complete), "\n";
217  
218 }
219  
220 1;