Spellfixoring RT#54388
[p5sagit/namespace-clean.git] / lib / namespace / clean.pm
index 6afa1d8..9ee2893 100644 (file)
@@ -5,8 +5,6 @@ use warnings;
 use strict;
 
 use vars qw( $STORAGE_VAR );
-use Sub::Name 0.04 qw(subname);
-use Sub::Identify 0.04 qw(sub_fullname);
 use Package::Stash 0.22;
 use B::Hooks::EndOfScope 0.07;
 
@@ -71,9 +69,9 @@ be a module exporting an C<import> method along with some functions:
 If you just want to C<-except> a single sub, you can pass it directly.
 For more than one value you have to use an array reference.
 
-=head2 Explicitely removing functions when your scope is compiled
+=head2 Explicitly removing functions when your scope is compiled
 
-It is also possible to explicitely tell C<namespace::clean> what packages
+It is also possible to explicitly tell C<namespace::clean> what packages
 to remove when the surrounding scope has finished compiling. Here is an
 example:
 
@@ -138,42 +136,69 @@ it is your responsibility to make sure it runs at that time.
 
 =cut
 
-my $RemoveSubs = sub {
+my $sub_utils_loaded;
+my $DebuggerRename = sub {
+  my ($f, $sub, $cleanee_stash, $deleted_stash) = @_;
+
+  if (! defined $sub_utils_loaded ) {
+    $sub_utils_loaded = do {
+      my $sn_ver = 0.04;
+      eval { require Sub::Name; Sub::Name->VERSION($sn_ver) }
+        or die "Sub::Name $sn_ver required when running under -d or equivalent: $@";
+
+      my $si_ver = 0.04;
+      eval { require Sub::Identify; Sub::Identify->VERSION($si_ver) }
+        or die "Sub::Identify $si_ver required when running under -d or equivalent: $@";
+
+      1;
+    } ? 1 : 0;
+  }
+
+  if ( Sub::Identify::sub_fullname($sub) eq ($cleanee_stash->name . "::$f") ) {
+    my $new_fq = $deleted_stash->name . "::$f";
+    Sub::Name::subname($new_fq, $sub);
+    $deleted_stash->add_symbol("&$f", $sub);
+  }
+};
 
+my $RemoveSubs = sub {
     my $cleanee = shift;
     my $store   = shift;
     my $cleanee_stash = Package::Stash->new($cleanee);
-    my $deleted_stash = Package::Stash->new("namespace::clean::deleted::$cleanee");
+    my $deleted_stash;
+
   SYMBOL:
     for my $f (@_) {
-        my $variable = "&$f";
+
         # ignore already removed symbols
         next SYMBOL if $store->{exclude}{ $f };
 
-        next SYMBOL unless $cleanee_stash->has_symbol($variable);
+        my $sub = $cleanee_stash->get_symbol("&$f")
+          or next SYMBOL;
 
-        if (ref(\$cleanee_stash->namespace->{$f}) eq 'GLOB') {
+        if ($^P and ref(\$cleanee_stash->namespace->{$f}) eq 'GLOB') {
             # convince the Perl debugger to work
             # it assumes that sub_fullname($sub) can always be used to find the CV again
             # since we are deleting the glob where the subroutine was originally
             # defined, that assumption no longer holds, so we need to move it
             # elsewhere and point the CV's name to the new glob.
-            my $sub = $cleanee_stash->get_symbol($variable);
-            if ( sub_fullname($sub) eq ($cleanee_stash->name . "::$f") ) {
-                my $new_fq = $deleted_stash->name . "::$f";
-                subname($new_fq, $sub);
-                $deleted_stash->add_symbol($variable, $sub);
-            }
+            $DebuggerRename->(
+              $f,
+              $sub,
+              $cleanee_stash,
+              $deleted_stash ||= Package::Stash->new("namespace::clean::deleted::$cleanee"),
+            );
         }
 
-        my ($scalar, $array, $hash, $io) = map {
-            $cleanee_stash->get_symbol($_ . $f)
+        my @symbols = map {
+            my $name = $_ . $f;
+            my $def = $cleanee_stash->get_symbol($name);
+            defined($def) ? [$name, $def] : ()
         } '$', '@', '%', '';
+
         $cleanee_stash->remove_glob($f);
-        for my $var (['$', $scalar], ['@', $array], ['%', $hash], ['', $io]) {
-            next unless defined $var->[1];
-            $cleanee_stash->add_symbol($var->[0] . $f, $var->[1]);
-        }
+
+        $cleanee_stash->add_symbol(@$_) for @symbols;
     }
 };