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

Undefining symbol tables and globs and things, oh my

by friedo (Prior)
on Aug 10, 2005 at 07:05 UTC ( #482531=perlquestion: print w/ replies, xml ) Need Help??
friedo has asked for the wisdom of the Perl Monks concerning the following question:

Submitted for your perusal:

#!/usr/bin/perl use Data::Dumper; my $var = "Yak"; ${ 'Yakkity::' . $var . '::a' } = 42; print "a = $Yakkity::Yak::a\n"; print Dumper \%Yakkity::Yak::; print "Undeffing symbol table\n"; undef %Yakkity::Yak::; print "a = $Yakkity::Yak::a\n"; print Dumper \%Yakkity::Yak::; print "Undeffing glob\n"; undef *Yakkity::Yak::a; print "a = $Yakkity::Yak::a\n"; print Dumper \%Yakkity::Yak::;

And the output:

a = 42 $VAR1 = { 'a' => *Yakkity::Yak::a }; Undeffing symbol table a = 42 $VAR1 = {}; Undeffing glob a = $VAR1 = {};

So, what's going on here? How does perl know how to find $Yakkity::Yak::a after the package's symbol table is gone? Shouldn't *Yakkity::Yak::a have been deleted with it? Or does another reference to it exist outside the symbol table? If so, where? Suppose I were creating dynamic packages at runtime and I want to make sure all the stuff in them gets destroyed. How would I ensure that?

Comment on Undefining symbol tables and globs and things, oh my
Select or Download Code
Re: Undefining symbol tables and globs and things, oh my
by PodMaster (Abbot) on Aug 10, 2005 at 07:58 UTC
    How does perl know how to find $Yakkity::Yak::a after the package's symbol table is gone?
    %Yakkity::Yak:: isn't exactly the symbol table. perldata says
    Perl uses an internal type called a typeglob to hold an entire symbol table entry. The type prefix of a typeglob is a *, because it represents all types.
    You should look into the Symbol module (Symbol::delete_package) and super search around here for discussions about the symbol table, as they're very informative

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      What you quoted is not relevant. What you quoted describes a symbol table entry, so it has no bearing on whether %Yakkity::Yak:: is a symbol table or not.
        Except that it is relevant. The symbol table for the Yakkity::Yak namespace is found in *Yakkity::Yak::.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Undefining symbol tables and globs and things, oh my
by dave_the_m (Parson) on Aug 10, 2005 at 12:44 UTC
    How does perl know how to find $Yakkity::Yak::a after the package's symbol table is gone?
    Because you have a compile-time reference to the typeglob associated with a, in the code print "a = $Yakkity::Yak::a\n";
    undeffing the symbol table just removes a ref to the typeglob; the typeglob itslef continues to exist. At run-time, perl ops do not lookup variables in stashes; instead the ops contain a pointer to the typeglob.

    Dave.

      To illustrate dave_the_m's and Bob9000's point, compare

      #!/usr/bin/perl $Yakkity::Yak::a = 42; print "a = $Yakkity::Yak::a\n"; print "Undeffing symbol table\n"; undef %Yakkity::Yak::; print "a = $Yakkity::Yak::a\n"; __END__ a = 42 Undeffing symbol table a = 42

      with

      #!/usr/bin/perl $Yakkity::Yak::a = 42; print "a = $Yakkity::Yak::a\n"; print "Undeffing symbol table\n"; undef %Yakkity::Yak::; print "a = ${'Yakkity::Yak::a'}\n"; # <- Hardcoded reference removed __END__ a = 42 Undeffing symbol table a =

      The last reference to $Yakkity::Yak::a keeps the symbol alive. If the reference to one built at run-time, the symbol disappears along with the table.

      Sorry, this post just explains what is seen, not how to work around it. Hopefully, someone has a solution.

Re: Undefining symbol tables and globs and things, oh my
by Bob9000 (Scribe) on Aug 10, 2005 at 12:46 UTC
    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.

    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.

Re: Undefining symbol tables and globs and things, oh my
by broquaint (Abbot) on Aug 10, 2005 at 15:02 UTC
    Suppose I were creating dynamic packages at runtime and I want to make sure all the stuff in them gets destroyed. How would I ensure that?
    If you want to delete a symbol table then use the delete_package function from Symbol e.g
    use Symbol 'delete_package'; use Data::Dumper; $Yakkity::Yak::a = 42; print "a = $Yakkity::Yak::a\n"; print Dumper \%Yakkity::Yak::; print "Undeffing symbol table\n"; delete_package 'Yakkity::Yak'; print "a = $Yakkity::Yak::a\n"; print Dumper \%Yakkity::Yak::; __output__ a = 42 $VAR1 = { 'a' => *Yakkity::Yak::a }; Undeffing symbol table a = $VAR1 = {};
    HTH

    _________
    broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://482531]
Approved by Corion
Front-paged by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-09-23 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (228 votes), past polls