I have worked a lot with changes of the symbol table for Safe::World, and looking in the replies that your node got, I think that none of them are the best way.
Well, say that my way is better is easy, soo I will show what happens in the 3 different types:
- DELETING FROM THE TABLE:
my $d = *{ 'main::destination::' };
delete $d->{foo};
This will just remove all the entry from the table, but wont ensure that the sub reference was really removed from the memory. Making test of full deletion of packages for Safe::World, is easy to see that if you don't undef explicity a reference from the memory it will stay there even if you clean the HASH of the symbol table.
But one of the problems here is that you remove all the references, soo, if you have a $foo and &foo, you will erase both.
undef
Just make a undef to the &sub actually works, but you will still have a *foo{CODE} value in the GLOB, what will use memory.
Note that undef is the standar way to do that, and is the recomended way, but you won't have the GLOB exactly as you have it before set the sub.
Symbol::gensym
In Perl is impossible to create a GLOB that exists only in the scope. Soo, gensym() creates a new GLOB at Symbol::GENx, where x is a number. Soo, it won't be cleanned when you go out of the scope. Again you will be just duplicating everything.
local()
Since a GLOB really need to be in the symbol table, local(*FOO) will create a GLOB that replaces the already existing GLOB in the symbol table for the given scope. Soo, after go out of the scope, the previous will be back. The biggest problem is when you make references to the local(*GLOB) after go out of the scope. Soo, if you make local(*FOO), and want to make something like a closure of it to $holder = \*FOO, it won't work.
Also for recursion or loop codes, the last local(*FOO) will change the previous sets.
NOW MAKEING A UNDO TO A GLOB:
Is easy, we just create a holder for the different types inside the GLOB, make a undef, than reset the values, unless the removed type:
*FOO = sub { print "SUB FOO OK!\n" } ;
$FOO = 123 ;
print ">>>>>>>>>> BEFORE RM:\n" ;
eval{ FOO() } ; warn($@) if $@ ;
print "CODE: " . *FOO{CODE} . "\n" ;
print "SCALAR: $FOO\n" ;
glob_rm( \*FOO , 'CODE' ) ;
print ">>>>>>>>>> AFTER RM:\n" ;
eval{ FOO() } ; warn($@) if $@ ;
print "CODE: " . *FOO{CODE} . "\n" ;
print "SCALAR: $FOO\n" ;
sub glob_rm {
my $glob = shift ;
my $type = uc( shift(@_) ) ;
my %holder ;
foreach my $tp ( qw(SCALAR ARRAY HASH CODE IO FORMAT) ) {
$holder{$tp} = *{$glob}{$tp} if $tp ne $type ;
}
if ($type eq 'SCALAR') { undef ${*{$glob}} ;}
elsif ($type eq 'ARRAY') { undef @{*{$glob}} ;}
elsif ($type eq 'HASH') { undef %{*{$glob}} ;}
elsif ($type eq 'CODE') { undef &{*{$glob}} ;}
elsif ($type eq 'IO') { close *{$glob} ;}
undef *{$glob} ;
foreach my $Key ( keys %holder ) { *{$glob} = $holder{$Key} ;}
}
This will print:
>>>>>>>>>> BEFORE RM:
SUB FOO OK!
CODE: CODE(0x1a72bac)
SCALAR: 123
>>>>>>>>>> AFTER RM:
Undefined subroutine &main::FOO called at tmp.pl line 17.
CODE:
SCALAR: 123
Note that this approach is to remove a reference type of a GLOB. If you want to overwrite one of the types, the best is to just set the GLOB with a reference.
Actually all of this depends of what you need. If you wan't speed, use just undef &FOO, but if you want to save memory I recomend the glob_rm() function.
Enjoy! ;-P
Graciliano M. P.
"Creativity is the expression of the liberty".
|