Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: What delete from symbol table really means? (Deleting typeglob of a specified package) by Anonymous Monk
in thread What delete from symbol table really means? (Deleting typeglob of a specified package) by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found