Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: What delete from symbol table really means? (Deleting typeglob of a specified package)

by Anonymous Monk
on Feb 16, 2015 at 10:54 UTC ( [id://1116858]=note: print w/replies, xml ) Need Help??


in reply to What delete from symbol table really means? (Deleting typeglob of a specified package)

The main question is: why this $n is still here with his well round value? The entry in the symbol table is gone the variable itself no!
It seems you think that the notation $n is a shorthand for something like $symbol_table_hash{n}. You're used to how Perl's hashes work. But, as you see, symbol tables are special, they're used at compile time and at run time Perl doesn't even look if key n is present in the symbol table (if it can avoid that). Why? For efficiency, of course. Perl COULD always look up things in symbol tables, in principle (at least I don't see why it couldn't). But it would be slower.

Also, the value 10 is not "inside" the symbol table hash. 10 is just somewhere in computer memory; the symbol table has information about how to find 10 (has the address of 10). Perl uses that information (the address) when it's compiling the code. When the code runs, the address is "compiled into" show_foo, and show_foo uses it directly, without fetching it from the symbol table.

That answer contains others choking examples about the matter:
$n = 123; delete $::{n}; eval '$n=456'; print $n; eval 'print $n';
Think of it this way: Perl only looks up the symbol table at run time if it must. So, for example, when you eval a string, Perl has to consult the symbol table, because at compile time 'print $n' was just a string, not code. OTOH, print $n (without quotes) was code, so the compiler could figure it out, and replace $n with its direct address in memory.

use feature 'say'; package Foo { our $n = 10; show_foo("After assignment"); delete $Foo::{'n'}; show_foo("After delete"); sub show_foo { say shift; say '$n => ', $n; say '$Foo::n => ', $Foo::n; say '$Foo::{n} => ', $Foo::{n}; say; } }

Output:
$n => 10 $Foo::n => 10 $Foo::{n} => *Foo::n After delete $n => 10 $Foo::n => 10 $Foo::{n} =>
As you can see, it does remove the typeglob, it's just that 10 is untouched, and show_foo still knows where it is. $n and $Foo::n are special and resolved when compiling show_foo.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1116858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found