Save, delete, and restore variables from/to a package's name space.

I came up with these after some experimentation when I recently needed to clear out a name space in order to "do" another script not in my control, then restore the defined variables much later.

Suggested improvements are welcome; this was my first time wading this deep into typeglobs and package namespaces.
# Return a hash that maps the name of symbols in a # namespace to an array of refs for all types for # which the name has a defined value. # A list of symbols may be specified; # default is all symbols in the name space. sub NameSpace::save { my $package = shift; my(%namerefs, $var, $type); no strict 'refs'; @_ = keys %{"${package}::"} if ! @_; foreach $var (@_) { $namerefs{$var} = []; my $fqvar = "${package}::$var"; # If the scalar for this variable name doesn't # already exist, *foo{SCALAR} will autovivify # the reference instead of returning undef, so # unlike the other types, we have to dereference # to find out if it exists. push(@{$namerefs{$var}}, *{$fqvar}{SCALAR}) if defined ${*{$fqvar}{SCALAR}}; foreach $type (qw(ARRAY HASH CODE IO)) { push(@{$namerefs{$var}}, *{$fqvar}{$type}) if defined *{$fqvar}{$type}; } } return \%namerefs; } # Remove the specified symbols from the namespace. # Default is to remove all. sub NameSpace::remove { my $package = shift; my(%namerefs, $var); no strict 'refs'; @_ = keys %{"${package}::"} if ! @_; foreach $var (@_) { delete ${"${package}::"}{$var}; } } # Restore values to symbols specified in a hash as returned # by NameSpace::save. sub NameSpace::restore { my($package, $namerefs) = @_; my($var, $ref); no strict 'refs'; foreach $var (keys %$namerefs) { my $fqvar = "${package}::$var"; foreach $ref (@{$namerefs->{$var}}) { *{$fqvar} = $ref; } } }

In reply to package namespace manipulation by knight

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.