/usr/share/perl5/Test2/Util/Stash.pm is in libtest2-suite-perl 0.000063-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 237 238 239 240 241 242 243 244 245 246 | package Test2::Util::Stash;
use strict;
use warnings;
our $VERSION = '0.000063';
use Carp qw/croak/;
use B;
our @EXPORT_OK = qw{
get_stash
get_glob
get_symbol
parse_symbol
purge_symbol
slot_to_sig sig_to_slot
};
use base 'Exporter';
my %SIGMAP = (
'&' => 'CODE',
'$' => 'SCALAR',
'%' => 'HASH',
'@' => 'ARRAY',
);
my %SLOTMAP = reverse %SIGMAP;
sub slot_to_sig { $SLOTMAP{$_[0]} || croak "unsupported slot: '$_[0]'" }
sub sig_to_slot { $SIGMAP{$_[0]} || croak "unsupported sigil: $_[0]" }
sub get_stash {
my $package = shift || caller;
no strict 'refs';
return \%{"${package}\::"};
}
sub get_glob {
my $sym = _parse_symbol(scalar(caller), @_);
no strict 'refs';
no warnings 'once';
return \*{"$sym->{package}\::$sym->{name}"};
}
sub parse_symbol { _parse_symbol(scalar(caller), @_) }
sub _parse_symbol {
my ($caller, $symbol, $package) = @_;
if (ref($symbol)) {
my $pkg = $symbol->{package};
croak "Symbol package ($pkg) and package argument ($package) do not match"
if $pkg && $package && $pkg ne $package;
$symbol->{package} ||= $caller;
return $symbol;
}
my ($sig, $pkg, $name) = ($symbol =~ m/^(\W?)(.*::)?([^:]+)$/)
or croak "Invalid symbol: '$symbol'";
# Normalize package, '::' becomes 'main', 'Foo::' becomes 'Foo'
$pkg = $pkg
? $pkg eq '::'
? 'main'
: substr($pkg, 0, -2)
: undef;
croak "Symbol package ($pkg) and package argument ($package) do not match"
if $pkg && $package && $pkg ne $package;
$sig ||= '&';
my $type = $SIGMAP{$sig} || croak "unsupported sigil: '$sig'";
my $real_package = $package || $pkg || $caller;
return {
name => $name,
sigil => $sig,
type => $type,
symbol => "${sig}${real_package}::${name}",
package => $real_package,
};
}
sub get_symbol {
my $sym = _parse_symbol(scalar(caller), @_);
my $name = $sym->{name};
my $type = $sym->{type};
my $package = $sym->{package};
my $symbol = $sym->{symbol};
my $stash = get_stash($package);
return undef unless exists $stash->{$name};
my $glob = get_glob($sym);
return *{$glob}{$type} if $type ne 'SCALAR' && defined(*{$glob}{$type});
if ($] < 5.010) {
return undef unless defined(*{$glob}{$type});
{
local ($@, $!);
local $SIG{__WARN__} = sub { 1 };
return *{$glob}{$type} if eval "package $package; my \$y = $symbol; 1";
}
return undef unless defined *{$glob}{$type};
return *{$glob}{$type} if defined ${*{$glob}{$type}};
return undef;
}
my $sv = B::svref_2object($glob)->SV;
return *{$glob}{$type} if $sv->isa('B::SV');
return undef unless $sv->isa('B::SPECIAL');
return *{$glob}{$type} if $B::specialsv_name[$$sv] ne 'Nullsv';
return undef;
}
sub purge_symbol {
my $sym = _parse_symbol(scalar(caller), @_);
local *GLOBCLONE = *{get_glob($sym)};
delete get_stash($sym->{package})->{$sym->{name}};
my $new_glob = get_glob($sym);
for my $type (qw/CODE SCALAR HASH ARRAY FORMAT IO/) {
next if $type eq $sym->{type};
my $ref = get_symbol({type => $type, name => 'GLOBCLONE', sigil => $SLOTMAP{$type}}, __PACKAGE__);
next unless $ref;
*$new_glob = $ref;
}
return *GLOBCLONE{$sym->{type}};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Util::Stash - Utilities for manipulating stashes and globs.
=head1 DESCRIPTION
This is a collection of utilities for manipulating and inspecting package
stashes and globs.
=head1 EXPORTS
=over 4
=item $stash = get_stash($package)
Gets the package stash. This is the same as C<$stash = \%Package::Name::>.
=item $sym_spec = parse_symbol($symbol)
=item $sym_spec = parse_symbol($symbol, $package)
Parse a symbol name, and return a hashref with info about the symbol.
C<$symbol> can be a simple name, or a fully qualified symbol name. The sigil is
optional, and C<&> is assumed if none is provided. If C<$symbol> is fully qualified,
and C<$package> is also provided, then the package of the symbol must match the
C<$package>.
Returns a structure like this:
return {
name => 'BAZ',
sigil => '$',
type => 'SCALAR',
symbol => '&Foo::Bar::BAZ',
package => 'Foo::Bar',
};
=item $glob_ref = get_glob($symbol)
=item $glob_ref = get_glob($symbol, $package)
Get a glob ref. Arguments are the same as for C<parse_symbol>.
=item $ref = get_symbol($symbol)
=item $ref = get_symbol($symbol, $package)
Get a reference to the symbol. Arguments are the same as for C<parse_symbol>.
=item $ref = purge_symbol($symbol)
=item $ref = purge_symbol($symbol, $package)
Completely remove the symbol from the package symbol table. Arguments are the
same as for C<parse_symbol>. A reference to the removed symbol is returned.
=item $sig = slot_to_sig($slot)
Convert a slot (like 'SCALAR') to a sigil (like '$').
=item $slot = sig_to_slot($sig)
Convert a sigil (like '$') to a slot (like 'SCALAR').
=back
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<http://github.com/Test-More/Test2-Suite/>.
=head1 MAINTAINERS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 AUTHORS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 COPYRIGHT
Copyright 2016 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut
|