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

In Loading/Unloading Modules?, toobored asked if you could unload a module. While working on this problem I came across Symbol::delete_package, which seemed like the answer, but Symbol points out that perl caches the symbol table and that this may cause things to break.

It seemed to me that this could be gotten around by using *{$package::$symbol} = \${$symtab->{$symbol}} to reset the cache, and sure enough it seems to work. If I use *bob:bob = \${{$::{'bob::'}}->{bob}} the cache gets reset and everything seems to work. However; if I use *{$package::$symbol} = \${$symtab->{$symbol}}, which I need to do, so I can loop over all symbols, it does not work.

Any ideas?

#!/usr/bin/perl use strict; use Symbol qw(delete_package); require 'bob.pm'; our $ref = $::{'bob::'}; print 'A ', \${$ref->{bob}}, ' ', ${$ref->{bob}}, "\n"; print 'A ', \$bob::bob, ' ', $bob::bob, "\n"; delete_package('bob'); print delete $INC{'bob.pm'}, "\n"; $ref = $::{'bob::'}; require 'bob.pm'; $ref = $::{'bob::'}; for (keys %$ref) { no strict 'refs'; *{"bob::$_"} = \${$ref->{$_}}; } print 'B ', \${$ref->{bob}}, ' ', ${$ref->{bob}}, "\n"; print 'B ', \$bob::bob, ' ', $bob::bob, "\n"; *bob::bob = \${$ref->{bob}}; print 'C ', \${$ref->{bob}}, ' ', ${$ref->{bob}}, "\n"; print 'C ', \$bob::bob, ' ', $bob::bob, "\n"; __END__ package bob; our $bob = "This is package bob"; printf "loading bob.pm: %s\n", $bob; 1; __OUTPUT__ loading bob.pm: This is package bob A SCALAR(0x816a2c8) This is package bob A SCALAR(0x816a2c8) This is package bob loading bob.pm: This is package bob B SCALAR(0x816a5a4) This is package bob B SCALAR(0x816a2c8) C SCALAR(0x816a5a4) This is package bob C SCALAR(0x816a5a4) This is package bob

And no, I don't know why I would want to do this.

-- gam3
A picture is worth a thousand words, but takes 200K.