in reply to Accessing constants via symbol table possible?
It may help to know what the const pragma does. It simply creates functions (subs) that are constant. A sub prototyped with () is defined to take no arguments and is then in-lined. Here's an evil method I wrote but never used in live code that may help you dig a little further:
sub const_override { my $name = shift; my $pkg; if ($name =~ /^(.*)::([^:]+)$/) { $pkg = $1; $name = $2; } else { $pkg = caller; } no strict 'refs'; if (!defined(*{"${pkg}::$name"})) { croak "A constant named '${pkg}::$name' has not been defined." +; } carp "Constant override for ${pkg}::$name not in BEGIN block may f +ail" if (defined($^S)); local $SIG{__WARN__} = sub { warn @_ if (join(" ", @_) !~ /Constant subroutine .* redefined +/); }; if (@_ == 1) { my $scalar = $_[0]; *{"${pkg}::$name"} = sub () { $scalar }; } elsif (@_) { my @list = @_; *{"${pkg}::$name"} = sub () { @list }; } else { my $set; *{"${pkg}::$name"} = sub () { }; } }
|
|---|