How does perl know how to find $Yakkity::Yak::a after the package's symbol table is gone?

It doesn't. However, it seems to compile $Yakkity::Yak::a as a hard reference to a specific entry in the symbol table, allowing that glob and the variables it references to continue existing after it has been removed from the table. Notice that after undeffing the symbol table, attempting to access the variable with a soft reference (i.e. ${"Yakkity::Yak::a"}), or with a direct reference in any code compiled later (i.e. eval '$Yakkity::Yak::a'), will fail...although compiling the new code (or accessing a soft reference) will create another variable named $Yakkity::Yak::a, which is unrelated to the one referenced by code compiled before the undeffing.

A demo:

#!/usr/bin/perl eval 'undef %x::; sub yak {$x::a}; $x::a = 1'; eval 'print "t1: ",keys %x::,"\n"'; eval 'print "t2: ",keys %x::,"\n"; $x::a = 2'; eval 'print "t3: ",yak,",",$x::a,"\n"';

This outputs the following:

t1: t2: a t3: 1,2

The first eval creates a glob *x::a in the symbol table, compiles both of its own $x::a accesses as references to that glob, then removes it from the symbol table upon executing the undef statement. As a result, there are no entries remaining in the symbol table, so t1 shows nothing. The third eval (t2) creates another glob named *x::a in the symbol table, prints the symbol table (showing *x::a still inside), and assigns the new $x::a a value. The fourth eval (t3) compiles its $x::a as a reference to the one currently in the symbol table, then accesses the original $x::a through a subroutine which was compiled with a reference to it, and prints both. (yak could just as well have been defined before any of the evals, since all that matters is that its compilation occurred before the undef was executed.)

Looking back at this post, I probably should have stopped after two sentences. But then, I had a little too much fun playing with this not to make a few notes.


In reply to Re: Undefining symbol tables and globs and things, oh my by Bob9000
in thread Undefining symbol tables and globs and things, oh my by friedo

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.