blib/lib/Devel/MAT/Tool/Symbols.pm

filter: Statementstmt 41.0% 39 / 95 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 65.0% 13 / 20 Podpod 0.0% 0 / 2 totaltotal 26.0% 52 / 200 CC 31 SCAR 41.8
CC
31
Cov
24%
__ANON__
132.0
_versionof
132.0
__ANON__
42.0
CRAP 65.1
« blib/lib/Devel/MAT/Tool/Strtab.pm ↑ Summary blib/lib/Devel/MAT/Tool/Find.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2017-2026 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Tool::Symbols 0.56;
7  
8 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use base qw( Devel::MAT::Tool );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use constant CMD => "symbols";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use constant CMD_DESC => "Display a list of the symbol table";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 C<Devel::MAT::Tool::Symbols> - display a list of the symbol table
21  
22 =head1 DESCRIPTION
23  
24 This C<Devel::MAT> tool displays a list names from the symbol table.
25  
26 =cut
27  
28 =head1 COMMANDS
29  
30 =for highlighter
31  
32 =head2 symbols
33  
34 pmat> symbols strict
35 $strict::VERSION
36 &strict::all_bits
37 &strict::all_explicit_bits
38 &strict::bits
39 &strict::import
40 &strict::unimport
41  
42 Prints a list of every name inside a symbol table hash ("stash"), starting
43 from the one given by name, or the toplevel default stash if none is provided.
44  
45 Takes the following named options:
46  
47 =over 4
48  
49 =item --recurse, -R
50  
51 Recursively show the inner symbols inside stashes.
52  
53 =back
54  
55 =cut
56  
57 0 ▶sub extract_symbols ( $stash, $prefix )
Subroutine extract_symbols: not called CC=3 SCAR=24.8
Pod undocumented
58 {
59 0 my @ret;
60 0 foreach my $key ( sort $stash->keys ) {
61 0 my $gv = $stash->value( $key );
62  
63 0 my $name;
64 0 ▶ if( $key =~ m/^([\0-\x1f])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^([\0-\x1f])/u) { } 0 0 0
65 0 $name = "{^" . chr(ord($1)+0x40) . substr( $key, 1 ) . "}";
66 }
67 else {
68 0 $name = $prefix . $key;
69 }
70  
71 0 push @ret, [ $gv, $name ];
72 }
73  
74 0 return @ret;
75 }
76  
77 0 ▶sub _show_symbol ( $name, $sv )
Subroutine _show_symbol: not called CC=1 SCAR=6.9
78 {
79 0 Devel::MAT::Cmd->printf( "%s at %s\n",
80 Devel::MAT::Cmd->format_symbol( $name, $sv ),
81 Devel::MAT::Cmd->format_sv( $sv ),
82 );
83 }
84  
85 5 ▶use constant CMD_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
86 recurse => { help => "recursively show inner symbols",
87 alias => "R" },
88 5 );
89  
90 5 ▶use constant CMD_ARGS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
91 { name => "start", help => "show symbols within this symbol, rather than %main::" },
92 5 );
93  
94 0 ▶sub run ( $self, $optsref, $start = undef )
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
95 0 {
96 0 my %opts = $optsref->%*;
97  
98 0 my $df = $self->df;
99  
100 0 my @queue;
101  
102 0 ▶ if( defined $start ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) { } 0 0 0
103 0 @queue = extract_symbols( $self->pmat->find_stash( $start ), $start . "::" );
104 }
105 else {
106 # Don't recurse into self-referential 'main::' symbol
107 0 @queue = grep { $_->[1] ne "main::" }
108 extract_symbols( $df->defstash, "" );
109  
110 # Also skip the "debug location" symbols, whatever those are
111 0 @queue = grep { $_->[1] !~ m/^_</ } @queue;
112 }
113  
114 0 ▶ Devel::MAT::Tool::more->paginate( sub ( $count ) {
Subroutine __ANON__: not called CC=11 SCAR=48.8
115 0 ▶ while( $count and @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count and @queue) 0 0 0
Condition0%0/3
$count and @queue
!ll&&!rl&&r
000
MC/DC0%0/2
$count and @queue
$count @queue
Truth table0%0/3
A and B
A= $countB= @queue
ABresult
0X0
100
111
116 0 $_ = shift @queue;
117 0 ▶ if( $_->[0]->isa( "Devel::MAT::SV::GLOB" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->[0]->isa('Devel::MAT::SV::GLOB')) { } 0 0 0
elsif ($opts{'recurse'} and $_->[0]->isa('Devel::MAT::SV::STASH')) { } 0 0 0
118 0 my ( $gv, $name ) = @$_;
119 0 ▶ _show_symbol( '$' . $name, $gv->scalar ), $count-- if $gv->scalar;
Branch: 1 branch
BranchTrueFalseTotal
if $gv->scalar 0 0 0
120 0 ▶ _show_symbol( '@' . $name, $gv->array ), $count-- if $gv->array;
Branch: 1 branch
BranchTrueFalseTotal
if $gv->array 0 0 0
121 0 ▶ _show_symbol( '%' . $name, $gv->hash ), $count-- if $gv->hash;
Branch: 1 branch
BranchTrueFalseTotal
if $gv->hash 0 0 0
122 0 ▶ _show_symbol( '&' . $name, $gv->code ), $count-- if $gv->code;
Branch: 1 branch
BranchTrueFalseTotal
if $gv->code 0 0 0
123  
124 0 ▶ unshift @queue, [ $gv->hash, $name ] if $gv->hash;
Branch: 1 branch
BranchTrueFalseTotal
if $gv->hash 0 0 0
125 }
126 ▶ elsif( $opts{recurse} and $_->[0]->isa( "Devel::MAT::SV::STASH" ) ) {
Condition0%0/3
$opts{"recurse"} and $_->[0]->isa("Devel::MAT::SV::STASH")
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"recurse"} and $_->[0]->isa("Devel::MAT::SV::STASH")
$opts{"recurse"} $_->[0]->isa("Devel::MAT::SV::STASH")
Truth table0%0/3
A and B
A= $opts{"recurse"}B= $_->[0]->isa("Devel::MAT::SV::STASH")
ABresult
0X0
100
111
127 0 my ( $stash, $prefix ) = @$_;
128 0 unshift @queue, extract_symbols( $stash, $prefix );
129 }
130 }
131  
132 0 return !!@queue;
133 0 } );
134 }
135  
136 package Devel::MAT::Tool::Symbols::_packages;
137  
138 5 ▶use base qw( Devel::MAT::Tool );
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 5 ▶use constant CMD => "packages";
Subroutine BEGIN: called CC=1 SCAR=0.0
141 5 ▶use constant CMD_DESC => "Display a list of the packages in the symbol table";
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 =head2 packages
144  
145 Prints a list of every package name in the symbol table.
146  
147 pmat> packages
148 package CORE at STASH(1) at 0x55cde0f74240
149 package CORE::GLOBAL at STASH(0) at 0x55cde0f74270
150 package Carp at STASH(4) at 0x55cde0fa1508
151 ...
152  
153 Takes the following named options:
154  
155 =over 4
156  
157 =item --versions, -V
158  
159 Include the value of the I<$VERSION> of each package, if relevant.
160  
161 =back
162  
163 =cut
164  
165 5 ▶use constant CMD_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
166 versions => { help => "show the \$VERSION of each package",
167 alias => "V" },
168 5 );
169  
170 0 ▶sub _versionof ( $stash )
Subroutine _versionof: not called CC=11 SCAR=48.8
171 {
172 # TODO: might be nice to have $stash->find_symbol
173 0 ▶ my $versiongv = $stash->value( 'VERSION' ) or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $versiongv = $stash->value("VERSION") or return "" 0 0 0
174 0 ▶ my $versionsv = $versiongv->scalar or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $versionsv = $versiongv->scalar or return "" 0 0 0
175  
176 0 my $rv;
177 my $version;
178 0 ▶ if( $versionsv->type eq "REF" and
Branch: 1 branch
BranchTrueFalseTotal
if ($versionsv->type eq 'REF' and ($rv = $versionsv->rv)->blessed and $rv->blessed->stashname eq 'version') { } 0 0 0
Condition0%0/6
$versionsv->type eq "REF" and ($rv = $versionsv->rv)->blessed and $rv->blessed->stashname eq "version"
!ll&&!rl&&r
000
$versionsv->type eq "REF" and ($rv = $versionsv->rv)->blessed
!ll&&!rl&&r
000
MC/DC0%0/3
$versionsv->type eq "REF" and ($rv = $versionsv->rv)->blessed and $rv->blessed->stashname eq "version"
$versionsv->type eq "REF" ($rv = $versionsv->rv)->blessed $rv->blessed->stashname eq "version"
Truth table0%0/4
A and B and C
A= $versionsv->type eq "REF"B= ($rv = $versionsv->rv)->blessedC= $rv->blessed->stashname eq "version"
ABCresult
0XX0
10X0
1100
1111
179 ( $rv = $versionsv->rv )->blessed and
180 $rv->blessed->stashname eq "version" ) {
181 # Stringify a "version" object
182 0 $versionsv = $rv->value( "original" );
183 0 ▶ $version = ( $versionsv->pv // $versionsv->nv // $versionsv->uv );
Condition0%0/6
($versionsv->pv // $versionsv->nv) // $versionsv->uv
l!l&&r!l&&!r
000
$versionsv->pv // $versionsv->nv
l!l&&r!l&&!r
000
MC/DC0%0/3
($versionsv->pv // $versionsv->nv) // $versionsv->uv
$versionsv->pv $versionsv->nv $versionsv->uv
Truth table0%0/4
A // B // C
A= $versionsv->pvB= $versionsv->nvC= $versionsv->uv
ABCresult
1XX1
01X1
0011
0000
184 0 ▶ $version =~ m/^v/ or $version = "v$version";
Branch: 1 branch
BranchTrueFalseTotal
$version =~ /^v/u or $version = "v$version" 0 0 0
185 }
186 else {
187 0 ▶ $version = $versionsv->pv // $versionsv->nv // $versionsv->uv;
Condition0%0/6
($versionsv->pv // $versionsv->nv) // $versionsv->uv
l!l&&r!l&&!r
000
$versionsv->pv // $versionsv->nv
l!l&&r!l&&!r
000
MC/DC0%0/3
($versionsv->pv // $versionsv->nv) // $versionsv->uv
$versionsv->pv $versionsv->nv $versionsv->uv
Truth table0%0/4
A // B // C
A= $versionsv->pvB= $versionsv->nvC= $versionsv->uv
ABCresult
1XX1
01X1
0011
0000
188 }
189 0 return " " . Devel::MAT::Cmd->format_value( $version );
190 }
191  
192 0 ▶sub run ( $self, $optsref )
Subroutine run: not called CC=2 SCAR=17.9
193 {
194 0 my %opts = $optsref->%*;
195  
196 my @queue = grep {
197 0 ▶ $_->[0]->isa( "Devel::MAT::SV::GLOB" ) and $_->[1] ne "main::"
Condition0%0/3
$_->[0]->isa('Devel::MAT::SV::GLOB') && $_->[1] ne 'main::'
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[0]->isa('Devel::MAT::SV::GLOB') && $_->[1] ne 'main::'
$_->[0]->isa('Devel::MAT::SV::GLOB') $_->[1] ne 'main::'
Truth table0%0/3
A && B
A= $_->[0]->isa('Devel::MAT::SV::GLOB')B= $_->[1] ne 'main::'
ABresult
0X0
100
111
198 } Devel::MAT::Tool::Symbols::extract_symbols( $self->df->defstash, "" );
199  
200 0 ▶ Devel::MAT::Tool::more->paginate( sub ( $count ) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
201 0 ▶ while( $count and @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count and @queue) 0 0 0
Condition0%0/3
$count and @queue
!ll&&!rl&&r
000
MC/DC0%0/2
$count and @queue
$count @queue
Truth table0%0/3
A and B
A= $countB= @queue
ABresult
0X0
100
111
202 0 $_ = shift @queue;
203 0 my ( $gv, $name ) = @$_;
204 0 ▶ next unless my $stash = $gv->hash;
Branch: 1 branch
BranchTrueFalseTotal
unless my $stash = $gv->hash 0 0 0
205 0 ▶ next unless $stash->isa( "Devel::MAT::SV::STASH" );
Branch: 1 branch
BranchTrueFalseTotal
unless $stash->isa("Devel::MAT::SV::STASH") 0 0 0
206  
207 Devel::MAT::Cmd->printf( "%s %s at %s\n",
208 Devel::MAT::Cmd->format_note( "package" ),
209 Devel::MAT::Cmd->format_symbol( $name =~ s/::$//r, $stash ) .
210 0 ▶ ( $opts{versions} ? _versionof( $stash ) : "" ),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'versions'} ? : 0 0 0
211 Devel::MAT::Cmd->format_sv( $stash ),
212 );
213 0 $count--;
214  
215 unshift @queue, grep {
216 0 $_->[0]->isa( "Devel::MAT::SV::GLOB" )
217 } Devel::MAT::Tool::Symbols::extract_symbols( $stash, $name );
218 }
219  
220 0 return !!@queue;
221 0 } );
222 }
223  
224 =head1 AUTHOR
225  
226 Paul Evans <leonerd@leonerd.org.uk>
227  
228 =cut
229  
230 5 0x55AA;
« blib/lib/Devel/MAT/Tool/Strtab.pm ↑ Summary blib/lib/Devel/MAT/Tool/Find.pm »