Reputation: 21625
I tried delete the name of a package variable from the main::
symbol table as below, but seems later code could still reference the variable, why it can be referenced after deletion? What's the effect of removing a name from symbol table?
$n = 10;
delete $main::{'n'};
print $n;
Upvotes: 2
Views: 148
Reputation: 98398
When perl code is compiled, globs for package variables/symbols are looked up (and created as necessary) and referenced directly from the compiled code.
So if you delete a symbol table entry $pkg::{n}
, all the already compiled code that used $pkg::n
, @pkg::n
, etc., still use the original glob. But do
/require
'd code or eval STRING
or symbolic references won't.
This code ends up with two *n globs; all the code compiled before the delete executes will reference the original one; all the code compiled after will reference the new one (and symbolic references will get whichever is in the symbol table at that point in runtime).
$n = 123;
delete $::{n};
eval '$n=456';
print $n;
eval 'print $n';
Another example:
$n = 123;
sub get_n { $n }
BEGIN { delete $::{n} }
$n = 456;
print get_n();
print $n;
Upvotes: 5