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