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.

In reply to More Loading and Unloading of Modules by gam3

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.