Accessing a package package variable after deleting a name from a symbol table in Perl?

I tried to remove the package variable name from the main:: symbol table, as shown below, but it seems later code can refer to the variable, why can it be specified after deletion? What is the effect of removing a name from a character table?

 $n = 10; delete $main::{'n'}; print $n; 
+2
perl
source share
1 answer

When perl code is compiled, globs for package variables / symbols are scanned (and generated as needed) and refer directly to the compiled code.

So, if you delete the character 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 links will not.

This code ends with two * n globs; all code compiled before the uninstall will be referenced to the source; all code compiled after that will be referenced to the new one (and symbolic links will be received depending on what is in the symbol table at this point at 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; 
+5
source share

All Articles