From: Peter Rabbitson Date: Tue, 26 Jul 2011 14:57:49 +0000 (+0200) Subject: Only ask for debugger bits when debugger detected X-Git-Tag: 0.21~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=017bd598b6a69299db79e5c59b35f10d047c2d81;p=p5sagit%2Fnamespace-clean.git Only ask for debugger bits when debugger detected --- diff --git a/Changes b/Changes index 984d869..aa157c4 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,6 @@ + - Only invoke the deleted sub stashing if we run udner a debugger + (avoid runtime penalty of Sub::Name/Sub::Identify) + [0.20] - Bump Package::Stash dependency to 0.22 to pull in a bugfix in Package::Stash::XS 0.19. diff --git a/lib/namespace/clean.pm b/lib/namespace/clean.pm index 57d96cb..f02bece 100644 --- a/lib/namespace/clean.pm +++ b/lib/namespace/clean.pm @@ -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; @@ -138,32 +136,58 @@ 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 @symbols = map {