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