File Coverage

blib/lib/namespace/clean.pm
Criterion Covered Total %
statement 66 68 97.0
branch 22 26 84.6
condition 2 3 66.6
subroutine 13 13 100.0
pod 3 4 75.0
total 106 114 92.9


line stmt bran cond sub pod time code
1             package namespace::clean;
2              
3 11     11   146178 use warnings;
  11         25  
  11         354  
4 11     11   58 use strict;
  11         22  
  11         926  
5              
6             our $VERSION = '0.26';
7             $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
8              
9             our $STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
10              
11 11     11   10332 use B::Hooks::EndOfScope 'on_scope_end';
  11         151434  
  11         86  
12              
13             # FIXME This is a crock of shit, needs to go away
14             # currently here to work around https://rt.cpan.org/Ticket/Display.html?id=74151
15             # kill with fire when PS::XS is *finally* fixed
16             BEGIN {
17 11     11   1748 my $provider;
18              
19 11 50       54 if ( $] < 5.008007 ) {
20 0         0 require Package::Stash::PP;
21 0         0 $provider = 'Package::Stash::PP';
22             }
23             else {
24 11         8375 require Package::Stash;
25 11         21685 $provider = 'Package::Stash';
26             }
27 11 50   1063 0 954 eval <<"EOS" or die $@;
  1063         12367  
28              
29             sub stash_for (\$) {
30             $provider->new(\$_[0]);
31             }
32              
33             1;
34              
35             EOS
36             }
37              
38 11     11   6511 use namespace::clean::_Util qw( DEBUGGER_NEEDS_CV_RENAME DEBUGGER_NEEDS_CV_PIVOT );
  11         31  
  11         13145  
39              
40             # Built-in debugger CV-retrieval fixups necessary before perl 5.15.5:
41             # since we are deleting the glob where the subroutine was originally
42             # defined, the assumptions below no longer hold.
43             #
44             # In 5.8.9 ~ 5.13.5 (inclusive) the debugger assumes that a CV can
45             # always be found under sub_fullname($sub)
46             # Workaround: use sub naming to properly name the sub hidden in the package's
47             # deleted-stash
48             #
49             # In the rest of the range ( ... ~ 5.8.8 and 5.13.6 ~ 5.15.4 ) the debugger
50             # assumes the name of the glob passed to entersub can be used to find the CV
51             # Workaround: realias the original glob to the deleted-stash slot
52             #
53             # Can not tie constants to the current value of $^P directly,
54             # as the debugger can be enabled during runtime (kinda dubious)
55             #
56              
57             my $RemoveSubs = sub {
58             my $cleanee = shift;
59             my $store = shift;
60             my $cleanee_stash = stash_for($cleanee);
61             my $deleted_stash;
62              
63             SYMBOL:
64             for my $f (@_) {
65              
66             # ignore already removed symbols
67             next SYMBOL if $store->{exclude}{ $f };
68              
69             my $sub = $cleanee_stash->get_symbol("&$f")
70             or next SYMBOL;
71              
72             my $need_debugger_fixup =
73             ( DEBUGGER_NEEDS_CV_RENAME or DEBUGGER_NEEDS_CV_PIVOT )
74             &&
75             $^P
76             &&
77             ref(my $globref = \$cleanee_stash->namespace->{$f}) eq 'GLOB'
78             &&
79             ( $deleted_stash ||= stash_for("namespace::clean::deleted::$cleanee") )
80             ;
81              
82             # convince the Perl debugger to work
83             # see the comment on top
84             if ( DEBUGGER_NEEDS_CV_RENAME and $need_debugger_fixup ) {
85             #
86             # Note - both get_subname and set_subname are only compiled when CV_RENAME
87             # is true ( the 5.8.9 ~ 5.12 range ). On other perls this entire block is
88             # constant folded away, and so are the definitions in ::_Util
89             #
90             # Do not be surprised that they are missing without DEBUGGER_NEEDS_CV_RENAME
91             #
92             namespace::clean::_Util::get_subname( $sub ) eq ( $cleanee_stash->name . "::$f" )
93             and
94             $deleted_stash->add_symbol(
95             "&$f",
96             namespace::clean::_Util::set_subname( $deleted_stash->name . "::$f", $sub ),
97             );
98             }
99             elsif ( DEBUGGER_NEEDS_CV_PIVOT and $need_debugger_fixup ) {
100             $deleted_stash->add_symbol("&$f", $sub);
101             }
102              
103             my @symbols = map {
104             my $name = $_ . $f;
105             my $def = $cleanee_stash->get_symbol($name);
106             defined($def) ? [$name, $def] : ()
107             } '$', '@', '%', '';
108              
109             $cleanee_stash->remove_glob($f);
110              
111             # if this perl needs no renaming trick we need to
112             # rename the original glob after the fact
113             DEBUGGER_NEEDS_CV_PIVOT
114             and
115             $need_debugger_fixup
116             and
117             *$globref = $deleted_stash->namespace->{$f};
118              
119             $cleanee_stash->add_symbol(@$_) for @symbols;
120             }
121             };
122              
123             sub clean_subroutines {
124 1     1 1 594 my ($nc, $cleanee, @subs) = @_;
125 1         4 $RemoveSubs->($cleanee, {}, @subs);
126             }
127              
128             sub import {
129 1016     1016   1184169 my ($pragma, @args) = @_;
130              
131 1016         1505 my (%args, $is_explicit);
132              
133             ARG:
134 1016         2866 while (@args) {
135              
136 2007 100       7233 if ($args[0] =~ /^\-/) {
137 1006         1589 my $key = shift @args;
138 1006         1606 my $value = shift @args;
139 1006         3907 $args{ $key } = $value;
140             }
141             else {
142 1001         1488 $is_explicit++;
143 1001         1887 last ARG;
144             }
145             }
146              
147 1016 100       3399 my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller;
148 1016 100       2045 if ($is_explicit) {
149             on_scope_end {
150 1001     1001   44258 $RemoveSubs->($cleanee, {}, @args);
151 1001         5844 };
152             }
153             else {
154              
155             # calling class, all current functions and our storage
156 15         49 my $functions = $pragma->get_functions($cleanee);
157 15         77 my $store = $pragma->get_class_store($cleanee);
158 15         510 my $stash = stash_for($cleanee);
159              
160             # except parameter can be array ref or single value
161 5         17 my %except = map {( $_ => 1 )} (
162             $args{ -except }
163 2         6 ? ( ref $args{ -except } eq 'ARRAY' ? @{ $args{ -except } } : $args{ -except } )
164 15 100       103 : ()
    100          
165             );
166              
167             # register symbols for removal, if they have a CODE entry
168 15         59 for my $f (keys %$functions) {
169 77 100       173 next if $except{ $f };
170 72 50       386 next unless $stash->has_symbol("&$f");
171 72         181 $store->{remove}{ $f } = 1;
172             }
173              
174             # register EOF handler on first call to import
175 15 100       68 unless ($store->{handler_is_installed}) {
176             on_scope_end {
177 14     14   6736 $RemoveSubs->($cleanee, $store, keys %{ $store->{remove} });
  14         74  
178 14         108 };
179 14         216 $store->{handler_is_installed} = 1;
180             }
181              
182 15         691 return 1;
183             }
184             }
185              
186             sub unimport {
187 1     1   7 my ($pragma, %args) = @_;
188              
189             # the calling class, the current functions and our storage
190 1 50       4 my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller;
191 1         2 my $functions = $pragma->get_functions($cleanee);
192 1         4 my $store = $pragma->get_class_store($cleanee);
193              
194             # register all unknown previous functions as excluded
195 1         5 for my $f (keys %$functions) {
196             next if $store->{remove}{ $f }
197 2 100 66     10 or $store->{exclude}{ $f };
198 1         3 $store->{exclude}{ $f } = 1;
199             }
200              
201 1         41 return 1;
202             }
203              
204             sub get_class_store {
205 16     16 1 36 my ($pragma, $class) = @_;
206 16         472 my $stash = stash_for($class);
207 16         47 my $var = "%$STORAGE_VAR";
208 16 100       267 $stash->add_symbol($var, {})
209             unless $stash->has_symbol($var);
210 16         127 return $stash->get_symbol($var);
211             }
212              
213             sub get_functions {
214 16     16 1 30 my ($pragma, $class) = @_;
215              
216 16         534 my $stash = stash_for($class);
217             return {
218 16         339 map { $_ => $stash->get_symbol("&$_") }
  79         561  
219             $stash->list_all_symbols('CODE')
220             };
221             }
222              
223             'Danger! Laws of Thermodynamics may not apply.'
224              
225             __END__