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
|
package Symbol::Get;
use strict;
use warnings;
use Call::Context ();
our $VERSION = '0.12';
=encoding utf-8
=head1 NAME
Symbol::Get - Read Perl’s symbol table programmatically
=head1 SYNOPSIS
package Foo;
our $name = 'haha';
our @list = ( 1, 2, 3 );
our %hash = ( foo => 1, bar => 2 );
use constant my_const => 'haha';
use constant my_const_list => qw( a b c );
sub doit { ... }
my $name_sr = Symbol::Get::get('$Foo::name'); # \$name
my $list_ar = Symbol::Get::get('@Foo::list'); # \@list
my $hash_hr = Symbol::Get::get('%Foo::hash'); $ \%hash
#Defaults to __PACKAGE__ if none is given:
my $doit_cr = Symbol::Get::get('&doit');
#Constants:
my $const_val = Symbol::Get::copy_constant('Foo::my_const');
my @const_list = Symbol::Get::copy_constant('Foo::my_const_list');
#The below return the same results since get_names() defaults
#to the current package if none is given.
my @names = Symbol::Get::get_names('Foo'); # keys %Foo::
my @names = Symbol::Get::get_names();
=head1 DESCRIPTION
Occasionally I have need to reference a variable programmatically.
This module facilitates that by providing an easy, syntactic-sugar-y,
read-only interface to the symbol table.
The SYNOPSIS above should pretty well cover usage.
=head1 ABOUT PERL CONSTANTS
Previous versions of this module endorsed constructions like:
my $const_sr = Symbol::Get::get('Foo::my_const');
my $const_ar = Symbol::Get::get('Foo::my_const_list');
… to read constants from the symbol table. This isn’t reliable across
Perl versions, though, so don’t do it; instead, use C<copy_constant()>.
=head1 SEE ALSO
=over 4
=item * L<Symbol::Values>
=back
=head1 LICENSE
This module is licensed under the MIT License.
=cut
use constant MIN_LIST_CONSTANT_PERL_VERSION => v5.20.0;
my %_sigil_to_type = qw(
$ SCALAR
@ ARRAY
% HASH
& CODE
);
my $sigils_re_txt = join('|', keys %_sigil_to_type);
sub get {
my ($var) = @_;
die 'Need a variable or constant name!' if !length $var;
my $sigil = substr($var, 0, 1);
goto \&_get_constant if $sigil =~ tr<A-Za-z_><>;
my $type = $_sigil_to_type{$sigil} or die "Unrecognized sigil: “$sigil”";
my $table_hr = _get_table_hr( substr($var, 1) );
return $table_hr && *{$table_hr}{$type};
}
sub copy_constant {
my ($var) = @_;
my $ref = _get_table_hr($var);
my @value;
if ('SCALAR' eq ref $ref) {
@value = ($$ref);
}
elsif ('ARRAY' eq ref $ref) {
@value = @$ref;
}
else {
@value = *{$ref}{'CODE'}->();
}
if (@value > 1) {
Call::Context::must_be_list();
return @value;
}
return $value[0];
}
#Referenced in tests.
sub _perl_supports_getting_list_constant_ref { return $^V ge MIN_LIST_CONSTANT_PERL_VERSION() }
sub _get_constant {
my ($var) = @_;
my $ref = _get_table_hr($var);
if ('SCALAR' ne ref($ref) && 'ARRAY' ne ref($ref)) {
my $msg = "$var is a regular symbol table entry, not a constant.";
if ( !_perl_supports_getting_list_constant_ref() ) {
$msg .= " Your Perl version ($^V) stores list constants in the symbol table as CODE references rather than ARRAYs; maybe that’s the issue?";
}
die $msg;
}
return $ref;
}
sub get_names {
my ($module) = @_;
$module ||= (caller 0)[0];
Call::Context::must_be_list();
my $table_hr = _get_module_table_hr($module);
die "Unknown namespace: “$module”" if !$table_hr;
return keys %$table_hr;
}
#----------------------------------------------------------------------
# To be completed if needed:
#
#sub list_sigils {
# my ($full_name) = @_;
#
# Call::Context::must_be_list();
#
# my ($module, $name) = ($full_name =~ m<(?:(.+)::)?(.+)>);
#
# my $table_hr = _get_table_for_module_name($module);
# my $glob = *{$table_hr};
#
# return
#}
#
#----------------------------------------------------------------------
sub _get_module_table_hr {
my ($module) = @_;
my @nodes = split m<::>, $module;
my $table_hr = \%main::;
my $pkg = q<>;
for my $n (@nodes) {
$table_hr = $table_hr->{"$n\::"};
$pkg .= "$n\::";
}
return $table_hr;
}
sub _get_table_hr {
my ($name) = @_;
$name =~ m<\A (?: (.+) ::)? ([^:]+ (?: ::)?) \z>x or do {
die "Invalid variable name: “$name”";
};
my $module = $1 || (caller 1)[0];
my $table_hr = _get_module_table_hr($module);
return $table_hr->{$2};
}
1;
|