Commit | Line | Data |
40aef9d6 |
1 | package namespace::clean; |
40aef9d6 |
2 | |
3 | use warnings; |
4 | use strict; |
5 | |
3c08bb19 |
6 | our $VERSION = '0.26'; |
18377f5b |
7 | $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases |
8 | |
727a1a2f |
9 | our $STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE'; |
fa84e425 |
10 | |
727a1a2f |
11 | use B::Hooks::EndOfScope 'on_scope_end'; |
9887772b |
12 | |
acb1d694 |
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 | my $provider; |
18 | |
19 | if ( $] < 5.008007 ) { |
20 | require Package::Stash::PP; |
21 | $provider = 'Package::Stash::PP'; |
22 | } |
23 | else { |
24 | require Package::Stash; |
25 | $provider = 'Package::Stash'; |
26 | } |
27 | eval <<"EOS" or die $@; |
28 | |
29 | sub stash_for (\$) { |
30 | $provider->new(\$_[0]); |
31 | } |
32 | |
33 | 1; |
34 | |
35 | EOS |
36 | } |
40aef9d6 |
37 | |
df4cbc4e |
38 | use namespace::clean::_Util qw( DEBUGGER_NEEDS_CV_RENAME DEBUGGER_NEEDS_CV_PIVOT ); |
80e4e267 |
39 | |
1ae6a568 |
40 | # Built-in debugger CV-retrieval fixups necessary before perl 5.15.5: |
80e4e267 |
41 | # since we are deleting the glob where the subroutine was originally |
1ae6a568 |
42 | # defined, the assumptions below no longer hold. |
80e4e267 |
43 | # |
1ae6a568 |
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) |
80e4e267 |
55 | # |
017bd598 |
56 | |
57 | my $RemoveSubs = sub { |
de673bbf |
58 | my $cleanee = shift; |
59 | my $store = shift; |
acb1d694 |
60 | my $cleanee_stash = stash_for($cleanee); |
017bd598 |
61 | my $deleted_stash; |
62 | |
de673bbf |
63 | SYMBOL: |
64 | for my $f (@_) { |
017bd598 |
65 | |
de673bbf |
66 | # ignore already removed symbols |
67 | next SYMBOL if $store->{exclude}{ $f }; |
de673bbf |
68 | |
017bd598 |
69 | my $sub = $cleanee_stash->get_symbol("&$f") |
70 | or next SYMBOL; |
226432f6 |
71 | |
80e4e267 |
72 | my $need_debugger_fixup = |
df4cbc4e |
73 | ( DEBUGGER_NEEDS_CV_RENAME or DEBUGGER_NEEDS_CV_PIVOT ) |
64c1bcfc |
74 | && |
80e4e267 |
75 | $^P |
76 | && |
77 | ref(my $globref = \$cleanee_stash->namespace->{$f}) eq 'GLOB' |
1ae6a568 |
78 | && |
79 | ( $deleted_stash ||= stash_for("namespace::clean::deleted::$cleanee") ) |
80e4e267 |
80 | ; |
81 | |
1ae6a568 |
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 ), |
80e4e267 |
97 | ); |
de673bbf |
98 | } |
1ae6a568 |
99 | elsif ( DEBUGGER_NEEDS_CV_PIVOT and $need_debugger_fixup ) { |
100 | $deleted_stash->add_symbol("&$f", $sub); |
101 | } |
d6aecfbf |
102 | |
d64ad6f8 |
103 | my @symbols = map { |
104 | my $name = $_ . $f; |
105 | my $def = $cleanee_stash->get_symbol($name); |
106 | defined($def) ? [$name, $def] : () |
ad4b1a60 |
107 | } '$', '@', '%', ''; |
d64ad6f8 |
108 | |
c86d6ae2 |
109 | $cleanee_stash->remove_glob($f); |
d64ad6f8 |
110 | |
80e4e267 |
111 | # if this perl needs no renaming trick we need to |
112 | # rename the original glob after the fact |
1ae6a568 |
113 | DEBUGGER_NEEDS_CV_PIVOT |
114 | and |
115 | $need_debugger_fixup |
116 | and |
117 | *$globref = $deleted_stash->namespace->{$f}; |
80e4e267 |
118 | |
d64ad6f8 |
119 | $cleanee_stash->add_symbol(@$_) for @symbols; |
de673bbf |
120 | } |
121 | }; |
53e92ec5 |
122 | |
fcfe7810 |
123 | sub clean_subroutines { |
124 | my ($nc, $cleanee, @subs) = @_; |
125 | $RemoveSubs->($cleanee, {}, @subs); |
126 | } |
127 | |
de673bbf |
128 | sub import { |
129 | my ($pragma, @args) = @_; |
53e92ec5 |
130 | |
de673bbf |
131 | my (%args, $is_explicit); |
fcfe7810 |
132 | |
133 | ARG: |
134 | while (@args) { |
135 | |
136 | if ($args[0] =~ /^\-/) { |
137 | my $key = shift @args; |
138 | my $value = shift @args; |
139 | $args{ $key } = $value; |
140 | } |
141 | else { |
142 | $is_explicit++; |
143 | last ARG; |
144 | } |
9b680ffe |
145 | } |
146 | |
fcfe7810 |
147 | my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller; |
de673bbf |
148 | if ($is_explicit) { |
aa2aafae |
149 | on_scope_end { |
de673bbf |
150 | $RemoveSubs->($cleanee, {}, @args); |
aa2aafae |
151 | }; |
9b680ffe |
152 | } |
de673bbf |
153 | else { |
154 | |
155 | # calling class, all current functions and our storage |
156 | my $functions = $pragma->get_functions($cleanee); |
157 | my $store = $pragma->get_class_store($cleanee); |
acb1d694 |
158 | my $stash = stash_for($cleanee); |
de673bbf |
159 | |
160 | # except parameter can be array ref or single value |
161 | my %except = map {( $_ => 1 )} ( |
162 | $args{ -except } |
163 | ? ( ref $args{ -except } eq 'ARRAY' ? @{ $args{ -except } } : $args{ -except } ) |
164 | : () |
165 | ); |
166 | |
167 | # register symbols for removal, if they have a CODE entry |
168 | for my $f (keys %$functions) { |
169 | next if $except{ $f }; |
c86d6ae2 |
170 | next unless $stash->has_symbol("&$f"); |
de673bbf |
171 | $store->{remove}{ $f } = 1; |
172 | } |
173 | |
174 | # register EOF handler on first call to import |
175 | unless ($store->{handler_is_installed}) { |
aa2aafae |
176 | on_scope_end { |
de673bbf |
177 | $RemoveSubs->($cleanee, $store, keys %{ $store->{remove} }); |
aa2aafae |
178 | }; |
de673bbf |
179 | $store->{handler_is_installed} = 1; |
180 | } |
181 | |
182 | return 1; |
183 | } |
9b680ffe |
184 | } |
185 | |
9b680ffe |
186 | sub unimport { |
fcfe7810 |
187 | my ($pragma, %args) = @_; |
9b680ffe |
188 | |
6c0ece9b |
189 | # the calling class, the current functions and our storage |
fcfe7810 |
190 | my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller; |
9b680ffe |
191 | my $functions = $pragma->get_functions($cleanee); |
192 | my $store = $pragma->get_class_store($cleanee); |
193 | |
6c0ece9b |
194 | # register all unknown previous functions as excluded |
9b680ffe |
195 | for my $f (keys %$functions) { |
196 | next if $store->{remove}{ $f } |
197 | or $store->{exclude}{ $f }; |
198 | $store->{exclude}{ $f } = 1; |
199 | } |
200 | |
201 | return 1; |
202 | } |
203 | |
9b680ffe |
204 | sub get_class_store { |
205 | my ($pragma, $class) = @_; |
acb1d694 |
206 | my $stash = stash_for($class); |
5460fcfb |
207 | my $var = "%$STORAGE_VAR"; |
c86d6ae2 |
208 | $stash->add_symbol($var, {}) |
209 | unless $stash->has_symbol($var); |
210 | return $stash->get_symbol($var); |
40aef9d6 |
211 | } |
212 | |
40aef9d6 |
213 | sub get_functions { |
214 | my ($pragma, $class) = @_; |
215 | |
acb1d694 |
216 | my $stash = stash_for($class); |
40aef9d6 |
217 | return { |
c86d6ae2 |
218 | map { $_ => $stash->get_symbol("&$_") } |
219 | $stash->list_all_symbols('CODE') |
40aef9d6 |
220 | }; |
221 | } |
222 | |
b3b7a821 |
223 | 'Danger! Laws of Thermodynamics may not apply.' |
224 | |
225 | __END__ |
226 | |
227 | =head1 NAME |
228 | |
229 | namespace::clean - Keep imports and functions out of your namespace |
230 | |
231 | =head1 SYNOPSIS |
232 | |
233 | package Foo; |
234 | use warnings; |
235 | use strict; |
236 | |
237 | use Carp qw(croak); # 'croak' will be removed |
238 | |
239 | sub bar { 23 } # 'bar' will be removed |
240 | |
241 | # remove all previously defined functions |
242 | use namespace::clean; |
243 | |
244 | sub baz { bar() } # 'baz' still defined, 'bar' still bound |
245 | |
246 | # begin to collection function names from here again |
247 | no namespace::clean; |
248 | |
249 | sub quux { baz() } # 'quux' will be removed |
250 | |
251 | # remove all functions defined after the 'no' unimport |
252 | use namespace::clean; |
253 | |
254 | # Will print: 'No', 'No', 'Yes' and 'No' |
255 | print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n"; |
256 | print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n"; |
257 | print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n"; |
258 | print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n"; |
259 | |
260 | 1; |
261 | |
262 | =head1 DESCRIPTION |
263 | |
264 | =head2 Keeping packages clean |
265 | |
266 | When you define a function, or import one, into a Perl package, it will |
267 | naturally also be available as a method. This does not per se cause |
268 | problems, but it can complicate subclassing and, for example, plugin |
269 | classes that are included via multiple inheritance by loading them as |
270 | base classes. |
271 | |
272 | The C<namespace::clean> pragma will remove all previously declared or |
273 | imported symbols at the end of the current package's compile cycle. |
274 | Functions called in the package itself will still be bound by their |
275 | name, but they won't show up as methods on your class or instances. |
276 | |
277 | By unimporting via C<no> you can tell C<namespace::clean> to start |
278 | collecting functions for the next C<use namespace::clean;> specification. |
279 | |
280 | You can use the C<-except> flag to tell C<namespace::clean> that you |
281 | don't want it to remove a certain function or method. A common use would |
282 | be a module exporting an C<import> method along with some functions: |
283 | |
284 | use ModuleExportingImport; |
285 | use namespace::clean -except => [qw( import )]; |
286 | |
287 | If you just want to C<-except> a single sub, you can pass it directly. |
288 | For more than one value you have to use an array reference. |
289 | |
253c58bc |
290 | =head3 Late binding caveat |
291 | |
292 | Note that the L<technique used by this module|/IMPLEMENTATION DETAILS> relies |
293 | on perl having resolved all names to actual code references during the |
294 | compilation of a scope. While this is almost always what the interpreter does, |
295 | there are some exceptions, notably the L<sort SUBNAME|perlfunc/sort> style of |
296 | the C<sort> built-in invocation. The following example will not work, because |
297 | C<sort> does not try to resolve the function name to an actual code reference |
298 | until B<runtime>. |
299 | |
300 | use MyApp::Utils 'my_sorter'; |
301 | use namespace::clean; |
302 | |
303 | my @sorted = sort my_sorter @list; |
304 | |
305 | You need to work around this by forcing a compile-time resolution like so: |
306 | |
307 | use MyApp::Utils 'sorter'; |
308 | use namespace::clean; |
309 | |
310 | my $my_sorter_cref = \&sorter; |
311 | |
312 | my @sorted = sort $my_sorter_cref @list; |
313 | |
b3b7a821 |
314 | =head2 Explicitly removing functions when your scope is compiled |
315 | |
316 | It is also possible to explicitly tell C<namespace::clean> what packages |
317 | to remove when the surrounding scope has finished compiling. Here is an |
318 | example: |
319 | |
320 | package Foo; |
321 | use strict; |
322 | |
323 | # blessed NOT available |
324 | |
325 | sub my_class { |
326 | use Scalar::Util qw( blessed ); |
327 | use namespace::clean qw( blessed ); |
328 | |
329 | # blessed available |
330 | return blessed shift; |
331 | } |
332 | |
333 | # blessed NOT available |
334 | |
335 | =head2 Moose |
336 | |
337 | When using C<namespace::clean> together with L<Moose> you want to keep |
338 | the installed C<meta> method. So your classes should look like: |
339 | |
340 | package Foo; |
341 | use Moose; |
342 | use namespace::clean -except => 'meta'; |
343 | ... |
344 | |
345 | Same goes for L<Moose::Role>. |
346 | |
347 | =head2 Cleaning other packages |
348 | |
349 | You can tell C<namespace::clean> that you want to clean up another package |
350 | instead of the one importing. To do this you have to pass in the C<-cleanee> |
351 | option like this: |
352 | |
353 | package My::MooseX::namespace::clean; |
354 | use strict; |
355 | |
356 | use namespace::clean (); # no cleanup, just load |
357 | |
358 | sub import { |
359 | namespace::clean->import( |
360 | -cleanee => scalar(caller), |
361 | -except => 'meta', |
362 | ); |
363 | } |
364 | |
365 | If you don't care about C<namespace::clean>s discover-and-C<-except> logic, and |
366 | just want to remove subroutines, try L</clean_subroutines>. |
367 | |
368 | =head1 METHODS |
369 | |
370 | =head2 clean_subroutines |
371 | |
372 | This exposes the actual subroutine-removal logic. |
373 | |
374 | namespace::clean->clean_subroutines($cleanee, qw( subA subB )); |
375 | |
376 | will remove C<subA> and C<subB> from C<$cleanee>. Note that this will remove the |
377 | subroutines B<immediately> and not wait for scope end. If you want to have this |
378 | effect at a specific time (e.g. C<namespace::clean> acts on scope compile end) |
379 | it is your responsibility to make sure it runs at that time. |
380 | |
381 | =head2 import |
382 | |
383 | Makes a snapshot of the current defined functions and installs a |
384 | L<B::Hooks::EndOfScope> hook in the current scope to invoke the cleanups. |
385 | |
386 | |
387 | =head2 unimport |
388 | |
389 | This method will be called when you do a |
390 | |
391 | no namespace::clean; |
392 | |
393 | It will start a new section of code that defines functions to clean up. |
394 | |
395 | =head2 get_class_store |
396 | |
397 | This returns a reference to a hash in a passed package containing |
398 | information about function names included and excluded from removal. |
399 | |
400 | =head2 get_functions |
401 | |
402 | Takes a class as argument and returns all currently defined functions |
403 | in it as a hash reference with the function name as key and a typeglob |
404 | reference to the symbol as value. |
405 | |
6c0ece9b |
406 | =head1 IMPLEMENTATION DETAILS |
407 | |
04312494 |
408 | This module works through the effect that a |
6c0ece9b |
409 | |
410 | delete $SomePackage::{foo}; |
411 | |
412 | will remove the C<foo> symbol from C<$SomePackage> for run time lookups |
413 | (e.g., method calls) but will leave the entry alive to be called by |
472d4b1e |
414 | already resolved names in the package itself. C<namespace::clean> will |
415 | restore and therefor in effect keep all glob slots that aren't C<CODE>. |
6c0ece9b |
416 | |
417 | A test file has been added to the perl core to ensure that this behaviour |
418 | will be stable in future releases. |
419 | |
420 | Just for completeness sake, if you want to remove the symbol completely, |
421 | use C<undef> instead. |
422 | |
40aef9d6 |
423 | =head1 SEE ALSO |
424 | |
705fe1b1 |
425 | L<B::Hooks::EndOfScope> |
40aef9d6 |
426 | |
04312494 |
427 | =head1 THANKS |
40aef9d6 |
428 | |
04312494 |
429 | Many thanks to Matt S Trout for the inspiration on the whole idea. |
40aef9d6 |
430 | |
fa84e425 |
431 | =head1 AUTHORS |
432 | |
433 | =over |
434 | |
435 | =item * |
436 | |
437 | Robert 'phaylon' Sedlacek <rs@474.at> |
438 | |
439 | =item * |
440 | |
441 | Florian Ragwitz <rafl@debian.org> |
442 | |
443 | =item * |
444 | |
445 | Jesse Luehrs <doy@tozt.net> |
446 | |
447 | =item * |
448 | |
449 | Peter Rabbitson <ribasushi@cpan.org> |
450 | |
b2e54862 |
451 | =item * |
452 | |
453 | Father Chrysostomos <sprout@cpan.org> |
454 | |
fa84e425 |
455 | =back |
456 | |
457 | =head1 COPYRIGHT AND LICENSE |
458 | |
b2e54862 |
459 | This software is copyright (c) 2011 by L</AUTHORS> |
fa84e425 |
460 | |
461 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. |