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

filter: Statementstmt 44.1% 30 / 68 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 1 totaltotal 28.9% 40 / 138 CC 25 SCAR 42.8
CC
25
Cov
27%
_init_hek_users
132.0
__ANON__
90.0
run
56.0
CRAP 72.0
« blib/lib/Devel/MAT.pm ↑ Summary blib/lib/Devel/MAT/Tool/Symbols.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, 2024-2026 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Tool::Strtab 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 Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use constant CMD => "strtab";
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use constant CMD_DESC => "Show information about the shared strings table (PL_strtab)";
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<Devel::MAT::Tool::Strtab> - show information about the shared string table
23  
24 =head1 DESCRIPTION
25  
26 This C<Devel::MAT> tool provides a command for listing entries from the shared
27 string table, and identifying places where those strings are used.
28  
29 =cut
30  
31 =head1 COMMANDS
32  
33 =for highlighter
34  
35 =cut
36  
37 =head2 strtab
38  
39 pmat> strtab
40 ...
41  
42 Prints a list of the keys from the shared string table. For each entry, it
43 attempts to identify other places where the string is shared.
44  
45 Takes the following named options:
46  
47 =over 4
48  
49 =item --count, -c MAX
50  
51 Show at most this number of values (default 10).
52  
53 =item --skip, -s COUNT
54  
55 Skip over this number of keys initially before starting to print.
56  
57 =item --no-sort, -n
58  
59 Don't bother to sort keys before printing. Keys will be printed in no
60 particular order (though the order will at least be stable between successive
61 invocations of the command during the same session).
62  
63 =back
64  
65 Takes the following positional arguments:
66  
67 =over 4
68  
69 =item *
70  
71 Optional filter pattern. If present, will only count and display keys matching
72 the given regexp. Must be specified in the form C</PATTERN/> with optional
73 trailing flags. The only permitted flags are C<adilmsux>.
74  
75 =back
76  
77 =cut
78  
79 5 ▶use constant CMD_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
80 count => { help => "maximum count of values to print",
81 type => "i",
82 alias => "c",
83 default => 10 },
84 skip => { help => "count of keys to skip initially before printing",
85 type => "i",
86 alias => "s" },
87 no_sort => { help => "don't sort keys before printing",
88 alias => "n" },
89 5 );
90  
91 5 ▶use constant CMD_ARGS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
92 { name => "filter", help => "optional pattern to filter keys by" },
93 5 );
94  
95 0 ▶sub _init_hek_users ( $self )
Subroutine _init_hek_users: not called CC=11 SCAR=48.8
96 {
97 0 my %hek_users;
98  
99 0 foreach my $sv ( $self->df->heap ) {
100 0 my $hek;
101  
102 match( $sv->type : eq ) {
103 case( "HASH" ) {
104 0 ▶ next if $sv == $self->df->strtab;
Branch: 1 branch
BranchTrueFalseTotal
if $sv == $self->df->strtab 0 0 0
105  
106 0 foreach my $key ( $sv->keys ) {
107 $hek = $sv->hek_at( $key ) and
108 0 ▶ push $hek_users{$hek}->@*, $sv;
Branch: 2 branches
BranchTrueFalseTotal
$hek = $sv->hek_at($key) and push @{$hek_users{$hek}}, $sv 0 0 0
$(Syntax::Keyword::Match/topic) eq 'GLOB' || $(Syntax::Keyword::Match/topic) eq 'CODE' ? : 0 0 0
Condition0%0/3
$(Syntax::Keyword::Match/topic) eq "GLOB" or $(Syntax::Keyword::Match/topic) eq "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "GLOB" or $(Syntax::Keyword::Match/topic) eq "CODE"
$(Syntax::Keyword::Match/topic) eq "GLOB" $(Syntax::Keyword::Match/topic) eq "CODE"
Truth table0%0/3
A or B
A= $(Syntax::Keyword::Match/topic) eq "GLOB"B= $(Syntax::Keyword::Match/topic) eq "CODE"
ABresult
1X1
011
000
109 }
110 }
111 case( "GLOB" ),
112 case( "CODE" ) {
113 $hek = $sv->name_hek and
114 0 ▶ push $hek_users{$hek}->@*, $sv;
Condition0%0/6
$hek = $sv->name_hek and push @{$hek_users{$hek}}, $sv
!ll&&!rl&&r
000
$(Syntax::Keyword::Match/topic) eq "SCALAR" and do { $hek = $sv->shared_hek and push @{$hek_users{$hek}}, $sv }
!ll&&!rl&&r
000
MC/DC0%0/2
$hek = $sv->name_hek and push @{$hek_users{$hek}}, $sv
$hek = $sv->name_hek push @{$hek_users{$hek}}, $sv
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "SCALAR" and do { $hek = $sv->shared_hek and push @{$hek_users{$hek}}, $sv }
$(Syntax::Keyword::Match/topic) eq "SCALAR" do { $hek = $sv->shared_hek and push @{$hek_users{$hek}}, $sv }
Truth table0%0/3
A and B
A= $hek = $sv->name_hekB= push @{$hek_users{$hek}}, $sv
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $(Syntax::Keyword::Match/topic) eq "SCALAR"B= do { $hek = $sv->shared_hek and push @{$hek_users{$hek}}, $sv }
ABresult
0X0
100
111
115 }
116 0 ▶ case( "SCALAR" ) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'HASH' ? : 0 0 0
117 $hek = $sv->shared_hek and
118 0 ▶ push $hek_users{$hek}->@*, $sv;
Branch: 1 branch
BranchTrueFalseTotal
$hek = $sv->shared_hek and push @{$hek_users{$hek}}, $sv 0 0 0
119 }
120 }
121 }
122  
123 0 return \%hek_users;
124 }
125  
126 0 ▶sub run ( $self, $optsref, $filter = undef )
Subroutine run: not called CC=7 SCAR=40.3
Pod undocumented
127 0 {
128 0 my %opts = $optsref->%*;
129  
130 0 my $strtab = $self->df->strtab;
131  
132 0 my @keys = $strtab->keys;
133 0 ▶ if( length $filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $filter) 0 0 0
134 0 ▶ $filter =~ m/^\/(.*)\/([adilmsux]*)$/ or
Branch: 1 branch
BranchTrueFalseTotal
$filter =~ m[^/(.*)/([adilmsux]*)$]u or die "Filter must be a /PATTERN.../ with optional flags" 0 0 0
135 die "Filter must be a /PATTERN.../ with optional flags";
136 0 my ( $pattern, $flags ) = ( $1, $2 );
137 0 my $re = qr/(?$flags:$pattern)/;
138 0 @keys = grep { $_ =~ $re } @keys;
139 }
140 0 ▶ @keys = sort @keys unless $opts{no_sort};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"no_sort"} 0 0 0
141 0 ▶ splice @keys, 0, $opts{skip} if $opts{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"skip"} 0 0 0
142  
143 0 ▶ my $hek_users = $self->{hek_users} //= $self->_init_hek_users;
Condition0%0/3
$self->{"hek_users"} //= $self->_init_hek_users
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"hek_users"} //= $self->_init_hek_users
$self->{"hek_users"} $self->_init_hek_users
Truth table0%0/3
A //= B
A= $self->{"hek_users"}B= $self->_init_hek_users
ABresult
1X1
011
000
144  
145 0 ▶ Devel::MAT::Tool::more->paginate( { pagesize => $opts{count} }, sub ( $count ) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
146 0 my @rows;
147 0 foreach my $key ( splice @keys, 0, $count ) {
148 0 my $hek = $strtab->hek_at( $key );
149  
150 0 push @rows, [
151 Devel::MAT::Cmd->format_value( $key, key => 1 ),
152 "HEK at " . Devel::MAT::Cmd->format_value( $hek, addr => 1 ),
153 ];
154  
155 0 ▶ my $users = $hek_users->{$hek} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $users = $hek_users->{$hek} or next 0 0 0
156  
157 0 foreach my $sv ( @$users ) {
158 match( $sv->type : eq ) {
159 case( "HASH" ) {
160 0 ▶ push @rows, [
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'GLOB' || $(Syntax::Keyword::Match/topic) eq 'CODE' ? : 0 0 0
Condition0%0/3
$(Syntax::Keyword::Match/topic) eq "GLOB" or $(Syntax::Keyword::Match/topic) eq "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "GLOB" or $(Syntax::Keyword::Match/topic) eq "CODE"
$(Syntax::Keyword::Match/topic) eq "GLOB" $(Syntax::Keyword::Match/topic) eq "CODE"
Truth table0%0/3
A or B
A= $(Syntax::Keyword::Match/topic) eq "GLOB"B= $(Syntax::Keyword::Match/topic) eq "CODE"
ABresult
1X1
011
000
161 " key of " . Devel::MAT::Cmd->format_sv( $sv ),
162 "",
163 ];
164 }
165 case( "GLOB" ),
166 case( "CODE" ) {
167 0 ▶ push @rows, [
Condition0%0/3
$(Syntax::Keyword::Match/topic) eq "SCALAR" and do { push @rows, [' shared PV of ' . "Devel::MAT::Cmd"->format_sv($sv), ""] }
!ll&&!rl&&r
000
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "SCALAR" and do { push @rows, [' shared PV of ' . "Devel::MAT::Cmd"->format_sv($sv), ""] }
$(Syntax::Keyword::Match/topic) eq "SCALAR" do { push @rows, [' shared PV of ' . "Devel::MAT::Cmd"->format_sv($sv), ""] }
Truth table0%0/3
A and B
A= $(Syntax::Keyword::Match/topic) eq "SCALAR"B= do { push @rows, [' shared PV of ' . "Devel::MAT::Cmd"->format_sv($sv), ""] }
ABresult
0X0
100
111
168 " name of " . Devel::MAT::Cmd->format_sv( $sv ),
169 "",
170 ];
171 }
172 0 ▶ case( "SCALAR" ) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'HASH' ? : 0 0 0
173 0 push @rows, [
174 " shared PV of " . Devel::MAT::Cmd->format_sv( $sv ),
175 "",
176 ];
177 }
178 }
179 }
180 }
181  
182 0 Devel::MAT::Cmd->print_table( \@rows, indent => 2 );
183  
184 0 my $morecount = @keys;
185 0 ▶ Devel::MAT::Cmd->printf( " ... (%d more)\n", $morecount ) if $morecount;
Branch: 1 branch
BranchTrueFalseTotal
if $morecount 0 0 0
186 0 return $morecount;
187 0 } );
188 }
189  
190 =head1 AUTHOR
191  
192 Paul Evans <leonerd@leonerd.org.uk>
193  
194 =cut
195  
196 5 0x55AA;
« blib/lib/Devel/MAT.pm ↑ Summary blib/lib/Devel/MAT/Tool/Symbols.pm »