# 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; } } }