Restrict debugger workaround to when DB::sub will be used
[p5sagit/namespace-clean.git] / lib / namespace / clean.pm
index 5d41c9d..80d017a 100644 (file)
@@ -3,7 +3,9 @@ package namespace::clean;
 use warnings;
 use strict;
 
-our $VERSION = '0.25';
+our $VERSION = '0.26';
+$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
+
 our $STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
 
 use B::Hooks::EndOfScope 'on_scope_end';
@@ -14,7 +16,7 @@ use B::Hooks::EndOfScope 'on_scope_end';
 BEGIN {
   my $provider;
 
-  if ( $] < 5.008007 ) {
+  if ( "$]" < 5.008007 ) {
     require Package::Stash::PP;
     $provider = 'Package::Stash::PP';
   }
@@ -35,43 +37,29 @@ EOS
 
 use namespace::clean::_Util qw( DEBUGGER_NEEDS_CV_RENAME DEBUGGER_NEEDS_CV_PIVOT );
 
-# Debugger fixup necessary before perl 5.15.5
-#
-# In perl 5.8.9-5.12, it assumes that sub_fullname($sub) can
-# always be used to find the CV again.
-# In perl 5.8.8 and 5.14, it assumes that the name of the glob
-# passed to entersub can be used to find the CV.
+# Built-in debugger CV-retrieval fixups necessary before perl 5.15.5:
 # since we are deleting the glob where the subroutine was originally
-# defined, those assumptions no longer hold.
+# defined, the assumptions below no longer hold.
 #
-# So in 5.8.9-5.12 we need to move it elsewhere and point the
-# CV's name to the new glob.
+# In 5.8.9 ~ 5.13.5 (inclusive) the debugger assumes that a CV can
+# always be found under sub_fullname($sub)
+# Workaround: use sub naming to properly name the sub hidden in the package's
+# deleted-stash
+#
+# In the rest of the range ( ... ~ 5.8.8 and 5.13.6 ~ 5.15.4 ) the debugger
+# assumes the name of the glob passed to entersub can be used to find the CV
+# Workaround: realias the original glob to the deleted-stash slot
+#
+# While the errors manifest themselves inside perl5db.pl, they are caused by
+# problems inside the interpreter.  If enabled ($^P & 0x01) and existent,
+# the DB::sub sub will be called by the interpreter for any sub call rather
+# that call the sub directly.  It is provided the real sub to call in $DB::sub,
+# but the value given has the issues described above.  We only have to enable
+# the workaround if DB::sub will be used.
+#
+# Can not tie constants to the current value of $^P directly,
+# as the debugger can be enabled during runtime (kinda dubious)
 #
-# In 5.8.8 and 5.14 we move it elsewhere and rename the
-# original glob by assigning the new glob back to it.
-my $sub_utils_loaded;
-my $DebuggerFixup = sub {
-  my ($f, $sub, $cleanee_stash, $deleted_stash) = @_;
-
-  if (DEBUGGER_NEEDS_CV_RENAME) {
-    #
-    # Note - both get_subname and set_subname are only compiled when CV_RENAME
-    # is true ( the 5.8.9 ~ 5.12 range ). On other perls this entire block is
-    # constant folded away, and so are the definitions in ::_Util
-    #
-    # Do not be surprised that they are missing without DEBUGGER_NEEDS_CV_RENAME
-    #
-    namespace::clean::_Util::get_subname( $sub ) eq  ( $cleanee_stash->name . "::$f" )
-      and
-    $deleted_stash->add_symbol(
-      "&$f",
-      namespace::clean::_Util::set_subname( $deleted_stash->name . "::$f", $sub ),
-    );
-  }
-  else {
-    $deleted_stash->add_symbol("&$f", $sub);
-  }
-};
 
 my $RemoveSubs = sub {
     my $cleanee = shift;
@@ -91,21 +79,35 @@ my $RemoveSubs = sub {
         my $need_debugger_fixup =
           ( DEBUGGER_NEEDS_CV_RENAME or DEBUGGER_NEEDS_CV_PIVOT )
             &&
-          $^P
+          $^P & 0x01
+            &&
+          defined &DB::sub
             &&
           ref(my $globref = \$cleanee_stash->namespace->{$f}) eq 'GLOB'
+            &&
+         ( $deleted_stash ||= stash_for("namespace::clean::deleted::$cleanee") )
         ;
 
-        if ($need_debugger_fixup) {
-          # convince the Perl debugger to work
-          # see the comment on top of $DebuggerFixup
-          $DebuggerFixup->(
-            $f,
-            $sub,
-            $cleanee_stash,
-            $deleted_stash ||= stash_for("namespace::clean::deleted::$cleanee"),
+        # convince the Perl debugger to work
+        # see the comment on top
+        if ( DEBUGGER_NEEDS_CV_RENAME and $need_debugger_fixup ) {
+          #
+          # Note - both get_subname and set_subname are only compiled when CV_RENAME
+          # is true ( the 5.8.9 ~ 5.12 range ). On other perls this entire block is
+          # constant folded away, and so are the definitions in ::_Util
+          #
+          # Do not be surprised that they are missing without DEBUGGER_NEEDS_CV_RENAME
+          #
+          namespace::clean::_Util::get_subname( $sub ) eq  ( $cleanee_stash->name . "::$f" )
+            and
+          $deleted_stash->add_symbol(
+            "&$f",
+            namespace::clean::_Util::set_subname( $deleted_stash->name . "::$f", $sub ),
           );
         }
+        elsif ( DEBUGGER_NEEDS_CV_PIVOT and $need_debugger_fixup ) {
+          $deleted_stash->add_symbol("&$f", $sub);
+        }
 
         my @symbols = map {
             my $name = $_ . $f;
@@ -117,10 +119,11 @@ my $RemoveSubs = sub {
 
         # if this perl needs no renaming trick we need to
         # rename the original glob after the fact
-        # (see commend of $DebuggerFixup
-        if (DEBUGGER_NEEDS_CV_PIVOT && $need_debugger_fixup) {
-          *$globref = $deleted_stash->namespace->{$f};
-        }
+        DEBUGGER_NEEDS_CV_PIVOT
+          and
+        $need_debugger_fixup
+          and
+        *$globref = $deleted_stash->namespace->{$f};
 
         $cleanee_stash->add_symbol(@$_) for @symbols;
     }
@@ -293,6 +296,30 @@ 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.
 
+=head3 Late binding caveat
+
+Note that the L<technique used by this module|/IMPLEMENTATION DETAILS> relies
+on perl having resolved all names to actual code references during the
+compilation of a scope. While this is almost always what the interpreter does,
+there are some exceptions, notably the L<sort SUBNAME|perlfunc/sort> style of
+the C<sort> built-in invocation. The following example will not work, because
+C<sort> does not try to resolve the function name to an actual code reference
+until B<runtime>.
+
+ use MyApp::Utils 'my_sorter';
+ use namespace::clean;
+
+ my @sorted = sort my_sorter @list;
+
+You need to work around this by forcing a compile-time resolution like so:
+
+ use MyApp::Utils 'sorter';
+ use namespace::clean;
+
+ my $my_sorter_cref = \&sorter;
+
+ my @sorted = sort $my_sorter_cref @list;
+
 =head2 Explicitly removing functions when your scope is compiled
 
 It is also possible to explicitly tell C<namespace::clean> what packages