http://qs1969.pair.com?node_id=1116851

Discipulus has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

In my learning curve, i touched symbol table for the second time. I read Of Symbol Tables and Globs and at the end i was sure to have understood. Now i'm reading Mastering Perl and precisely a chapter dedicated to Symbol table and typeglobs (chapter 8). There is an example that left me full of doubts; here i propose you a semplified version:
#!/usr/bin/perl package Foo { $n = 10; show_foo( "After assignment" ); delete $Foo::{'n'}; show_foo( "After delete" ); sub show_foo { print "-" x 10, $_[0], "-" x 10, "\n"; print "\$n is $n\n"; foreach my $name ( keys %Foo:: ) { print "$name\n"; } } }#end of package Foo ##OUT ----------After assignment---------- $n is 10 show_foo n ----------After delete---------- $n is 10 show_foo
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!
The output of the example is correct but not explained by the book and (as ysth noticed see page 130 seems i'm not the only one to be confused).

When i asked this into the chat, the kind monk ysth pointed me to the first clearly stated sentence about the matter: an answer from ysth on another site.

That answer contains others choking examples about the matter:
$n = 123; delete $::{n}; eval '$n=456'; print $n; eval 'print $n'; #OUT 123456
and
$n = 123; sub get_n { $n } BEGIN { delete $::{n} } $n = 456; print get_n(); print $n; #OUT 123456
So there is a way to safely access and modify (so delete too) a specified entry in the symbol table or a specified slot in it (using the so called *foo{THING} notation). There is a gently way? or I need to nuke the package at whole? as in:
#!/usr/bin/perl use Symbol qw(delete_package); package Foo { $n = 10; show_foo( "After assignment" ); sub show_foo { print "-" x 10, $_[0], "-" x 10, "\n"; print "\$n is $n\n"; foreach my $name ( keys %Foo:: ) { print "$name\n"; } } }#end of package Foo delete_package('Foo'); # package Foo again package Foo{ show_foo( "After delete" ); } #OUT ----------After assignment---------- $n is 10 show_foo n Undefined subroutine & called at ...

The Symbol of the core module have intimidating BUGS section about the delete_package call.

Thanks for the attention.

L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.