blib/lib/Devel/Trepan/CmdProcessor/Command/Info_Subcmd/Packages.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 202 CC 22 SCAR 52.0
CC
22
Cov
0%
run
342.0
parse_options
30.0
complete
2.0
CRAP 180.9
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014 Rocky Bernstein <rocky@cpan.org>
3 use warnings; no warnings 'redefine'; no warnings 'once';
4 use rlib '../../../../..';
5  
6 package Devel::Trepan::CmdProcessor::Command::Info::Packages;
7  
8 use Devel::Trepan::CmdProcessor::Command::Subcmd::Core;
9 use Getopt::Long qw(GetOptionsFromArray);
10  
11 use strict;
12 our (@ISA, @SUBCMD_VARS);
13 # Values inherited from parent
14 use vars @Devel::Trepan::CmdProcessor::Command::Subcmd::SUBCMD_VARS;
15  
16 ## FIXME: do automatically.
17 our $CMD = "info packages";
18  
19 unless (@ISA) {
20 eval <<"EOE";
21 use constant MAX_ARGS => undef; # Need at most this many - undef -> unlimited.
22 EOE
23 }
24 @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd);
25 =pod
26  
27 =head2 Synopsis:
28  
29 =cut
30 our $HELP = <<'HELP';
31 =pod
32  
33 B<info packages> [I<options>] [I<match>]
34  
35 options:
36  
37 -e | --exact
38 -p | --prefix
39 -r | --regexp
40 -f | --files
41 -s | --subs
42  
43 The default is C<--prefix>
44  
45 Give package names and optionally the file(s) that package is in for
46 packages matching I<match>. Options control how to interpret the the
47 match pattern.
48  
49 =head2 Examples:
50  
51 info packages Tie:: # match all packages that start with Tie::
52 # e.g. Tie::ExtraHash and Tie::Hash
53 info packages -p Tie:: # same as above
54 info packages -r ^Tie:: # same as above
55 info packages -s Tie:: # same as above, but list the subs
56 # of each package along with the package
57 info packages -e Tie::Hash # match exactly Tie::Hash
58 info packages -e -f Tie::Hash # same as above but show the file(s) where
59 # the package is defined
60 info packages -r ::Tie$ # match Tie only at the end,
61 # e.g. ReadLine::Tie
62 info packages # List all packages
63  
64 =head2 See also:
65  
66 L<C<info functions>|Devel::Trepan::CmdProcessor::Command::Info::Functions>, and
67 L<C<complete>|Devel::Trepan::CmdProcessor::Command::Complete>.
68  
69 =cut
70 HELP
71  
72 our $SHORT_HELP = 'All function names, or those matching REGEXP';
73 our $MIN_ABBREV = length('pa');
74  
75 sub complete($$) {
76 my ($self, $prefix) = @_;
77 my @pkgs = Devel::Trepan::Complete::complete_packages($prefix);
78 my @opts = (qw(-r --regexp -p --prefix -s --subs -f --files),
79 @pkgs);
80 Devel::Trepan::Complete::complete_token(\@opts, $prefix) ;
81 }
82  
83 my $DEFAULT_OPTIONS = {
84 exact => 0,
85 prefix => 0,
86 regexp => 0,
87 files => 0,
88 funcs => 0,
89 };
90  
91 sub parse_options($$)
92 {
93 my ($self, $args) = @_;
94 my %opts = %$DEFAULT_OPTIONS;
95 my $result = &GetOptionsFromArray($args,
96 '-e' => \$opts{exact},
97 '--exact' => \$opts{exact},
98 '-r' => \$opts{regexp},
99 '--regexp' => \$opts{regexp},
100 '-f' => \$opts{files},
101 '--files' => \$opts{files},
102 '-p' => \$opts{prefix},
103 '--prefix' => \$opts{prefix},
104 '-s' => \$opts{subs},
105 '--subs' => \$opts{subs}
106 );
107 # Option consistency checking
108 my $count = $opts{exact} + $opts{regexp} + $opts{prefix};
109 if ($count == 0) {
110 $opts{prefix} = 1;
111 } elsif ($count > 1) {
112 if ($opts{regexp}) {
113 $self->{proc}->errmsg("regexp option used with prefix and/or exact; regexp used");
114 $opts{prefix} = $opts{exact} = 0;
115 } elsif ($opts{prefix}) {
116 $self->{proc}->errmsg("prefix used with exact; prefix used");
117 $opts{exact} = 0;
118 }
119 }
120  
121 \%opts;
122  
123 }
124  
125 # FIXME combine with Command::columnize_commands
126 use Array::Columnize;
127 sub columnize_pkgs($$)
128 {
129 my ($proc, $commands) = @_;
130 my $width = $proc->{settings}->{maxwidth};
131 my $r = Array::Columnize::columnize($commands,
132 {displaywidth => $width,
133 colsep => ' ',
134 ljust => 1,
135 lineprefix => ' '});
136 chomp $r;
137 return $r;
138 }
139  
140 sub run($$)
141 {
142 my ($self, $args) = @_;
143 my @args = @$args;
144 my $options = parse_options($self, \@args);
145 my $proc = $self->{proc};
146 my $match = undef;
147  
148 if (@args == 3) {
149 $match = $args[2];
150 }
151  
152 my %pkgs;
153 foreach my $function (keys %DB::sub) {
154 my @parts = split('::', $function);
155 if (scalar @parts > 1) {
156 my $func = pop(@parts);
157 my $pkg = join('::', @parts);
158 $pkgs{$pkg} ||= [{}, {}];
159 if ($options->{files}) {
160 my $file_range = $DB::sub{$function};
161 if ($file_range =~ /^(.+):(\d+-\d+)/) {
162 my ($filename, $range) = ($1, $2);
163 my $files = $pkgs{$pkg}->[0];
164 $files->{$filename} = 1;
165 $pkgs{$pkg}->[0] = $files;
166 }
167 }
168 if ($options->{subs}) {
169 my $funcs = $pkgs{$pkg}->[1];
170 $funcs->{$func} = 1;
171 $pkgs{$pkg}->[1] = $funcs;
172 }
173  
174 }
175 }
176 my @pkgs = keys %pkgs;
177 if ($options->{regexp}) {
178 @pkgs = grep /$match/, @pkgs if defined $match;
179 } elsif ($options->{prefix}) {
180 @pkgs = grep /^$match/, @pkgs if defined $match;
181 } else {
182 @pkgs = grep /^$match$/, @pkgs if defined $match;
183 }
184 if (scalar @pkgs) {
185 if ($options->{files} || $options->{subs}) {
186 for my $pkg (sort @pkgs) {
187 if ($options->{subs}) {
188 my $subs = $pkgs{$pkg}->[1];
189 my @subs = sort keys %$subs;
190 $proc->section($pkg);
191 if (scalar @subs) {
192 my $msg = columnize_pkgs($proc, \@subs);
193 $proc->msg($msg);
194 } else {
195 $proc->msg($pkg);
196 }
197 }
198 if ($options->{files}) {
199 my $filename = $pkgs{$pkg}->[0];
200 my @files = sort keys %$filename;
201 if (scalar @files) {
202 my $file_str = @files == 1 ? 'file' : 'files';
203 my $msg = sprintf("%s is in %s %s", $pkg, $file_str,
204 join(', ', @files));
205 $proc->msg($msg);
206 } else {
207 $proc->msg($pkg);
208 }
209 }
210 }
211 } else {
212 @pkgs = sort @pkgs;
213 my $msg = columnize_pkgs($proc, \@pkgs);
214 $proc->msg($msg);
215 }
216 } else {
217 $proc->msg('No matching package');
218 }
219 }
220  
221 unless (caller) {
222 # Demo it.
223 require Devel::Trepan::CmdProcessor::Mock;
224 my $proc = Devel::Trepan::CmdProcessor->new(undef, 'bogus');
225 my $cmd = __PACKAGE__->new($proc);
226 $cmd->{proc} = $proc;
227 my $frame_ary = Devel::Trepan::CmdProcessor::Mock::create_frame();
228 $proc->frame_setup($frame_ary);
229 $proc->{settings}{highlight} = 0;
230 %DB::sub = qw(main::gcd 1);
231 $cmd->run([]);
232  
233 # require_relative '../../mock'
234 # my($dbgr, $parent_cmd) = MockDebugger::setup('show');
235 # $cmd = __PACKAGE__->new(parent_cmd);
236 # $cmd->run(@$cmd->prefix);
237 }
238  
239 # Suppress a "used-once" warning;
240 $HELP || scalar @SUBCMD_VARS;