1 package namespace::clean;
5 namespace::clean - Keep imports and functions out of your namespace
12 use vars qw( $VERSION $STORAGE_VAR $SCOPE_HOOK_KEY $SCOPE_EXPLICIT );
13 use Symbol qw( qualify_to_ref );
14 use B::Hooks::EndOfScope;
23 $STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
31 use Carp qw(croak); # 'croak' will be removed
33 sub bar { 23 } # 'bar' will be removed
35 # remove all previously defined functions
38 sub baz { bar() } # 'baz' still defined, 'bar' still bound
40 # begin to collection function names from here again
43 sub quux { baz() } # 'quux' will be removed
45 # remove all functions defined after the 'no' unimport
48 # Will print: 'No', 'No', 'Yes' and 'No'
49 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
50 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
51 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
52 print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
58 =head2 Keeping packages clean
60 When you define a function, or import one, into a Perl package, it will
61 naturally also be available as a method. This does not per se cause
62 problems, but it can complicate subclassing and, for example, plugin
63 classes that are included via multiple inheritance by loading them as
66 The C<namespace::clean> pragma will remove all previously declared or
67 imported symbols at the end of the current package's compile cycle.
68 Functions called in the package itself will still be bound by their
69 name, but they won't show up as methods on your class or instances.
71 By unimporting via C<no> you can tell C<namespace::clean> to start
72 collecting functions for the next C<use namespace::clean;> specification.
74 You can use the C<-except> flag to tell C<namespace::clean> that you
75 don't want it to remove a certain function or method. A common use would
76 be a module exporting an C<import> method along with some functions:
78 use ModuleExportingImport;
79 use namespace::clean -except => [qw( import )];
81 If you just want to C<-except> a single sub, you can pass it directly.
82 For more than one value you have to use an array reference.
84 =head2 Explicitely removing functions when your scope is compiled
86 It is also possible to explicitely tell C<namespace::clean> what packages
87 to remove when the surrounding scope has finished compiling. Here is an
93 # blessed NOT available
96 use Scalar::Util qw( blessed );
97 use namespace::clean qw( blessed );
100 return blessed shift;
103 # blessed NOT available
107 When using C<namespace::clean> together with L<Moose> you want to keep
108 the installed C<meta> method. So your classes should look like:
112 use namespace::clean -except => 'meta';
115 Same goes for L<Moose::Role>.
117 =head2 Cleaning other packages
119 You can tell C<namespace::clean> that you want to clean up another package
120 instead of the one importing. To do this you have to pass in the C<-cleanee>
123 package My::MooseX::namespace::clean;
126 use namespace::clean (); # no cleanup, just load
129 namespace::clean->import(
130 -cleanee => scalar(caller),
135 If you don't care about C<namespace::clean>s discover-and-C<-except> logic, and
136 just want to remove subroutines, try L</clean_subroutines>.
140 You shouldn't need to call any of these. Just C<use> the package at the
145 =head2 clean_subroutines
147 This exposes the actual subroutine-removal logic.
149 namespace::clean->clean_subroutines($cleanee, qw( subA subB ));
151 will remove C<subA> and C<subB> from C<$cleanee>. Note that this will remove the
152 subroutines B<immediately> and not wait for scope end. If you want to have this
153 effect at a specific time (e.g. C<namespace::clean> acts on scope compile end)
154 it is your responsibility to make sure it runs at that time.
158 my $RemoveSubs = sub {
165 # ignore already removed symbols
166 next SYMBOL if $store->{exclude}{ $f };
169 # keep original value to restore non-code slots
170 { no warnings 'uninitialized'; # fix possible unimports
171 local *__tmp = *{ ${ "${cleanee}::" }{ $f } };
172 delete ${ "${cleanee}::" }{ $f };
176 # restore non-code slots to symbol
177 for my $t (qw( SCALAR ARRAY HASH IO FORMAT )) {
178 next SLOT unless defined *__tmp{ $t };
179 *{ "${cleanee}::$f" } = *__tmp{ $t };
184 sub clean_subroutines {
185 my ($nc, $cleanee, @subs) = @_;
186 $RemoveSubs->($cleanee, {}, @subs);
191 Makes a snapshot of the current defined functions and installs a
192 L<B::Hooks::EndOfScope> hook in the current scope to invoke the cleanups.
197 my ($pragma, @args) = @_;
199 my (%args, $is_explicit);
204 if ($args[0] =~ /^\-/) {
205 my $key = shift @args;
206 my $value = shift @args;
207 $args{ $key } = $value;
215 my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller;
218 $RemoveSubs->($cleanee, {}, @args);
223 # calling class, all current functions and our storage
224 my $functions = $pragma->get_functions($cleanee);
225 my $store = $pragma->get_class_store($cleanee);
227 # except parameter can be array ref or single value
228 my %except = map {( $_ => 1 )} (
230 ? ( ref $args{ -except } eq 'ARRAY' ? @{ $args{ -except } } : $args{ -except } )
234 # register symbols for removal, if they have a CODE entry
235 for my $f (keys %$functions) {
236 next if $except{ $f };
237 next unless $functions->{ $f }
238 and *{ $functions->{ $f } }{CODE};
239 $store->{remove}{ $f } = 1;
242 # register EOF handler on first call to import
243 unless ($store->{handler_is_installed}) {
245 $RemoveSubs->($cleanee, $store, keys %{ $store->{remove} });
247 $store->{handler_is_installed} = 1;
256 This method will be called when you do a
260 It will start a new section of code that defines functions to clean up.
265 my ($pragma, %args) = @_;
267 # the calling class, the current functions and our storage
268 my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller;
269 my $functions = $pragma->get_functions($cleanee);
270 my $store = $pragma->get_class_store($cleanee);
272 # register all unknown previous functions as excluded
273 for my $f (keys %$functions) {
274 next if $store->{remove}{ $f }
275 or $store->{exclude}{ $f };
276 $store->{exclude}{ $f } = 1;
282 =head2 get_class_store
284 This returns a reference to a hash in a passed package containing
285 information about function names included and excluded from removal.
289 sub get_class_store {
290 my ($pragma, $class) = @_;
292 return \%{ "${class}::${STORAGE_VAR}" };
297 Takes a class as argument and returns all currently defined functions
298 in it as a hash reference with the function name as key and a typeglob
299 reference to the symbol as value.
304 my ($pragma, $class) = @_;
307 map { @$_ } # key => value
308 grep { *{ $_->[1] }{CODE} } # only functions
309 map { [$_, qualify_to_ref( $_, $class )] } # get globref
310 grep { $_ !~ /::$/ } # no packages
311 do { no strict 'refs'; keys %{ "${class}::" } } # symbol entries
315 =head1 IMPLEMENTATION DETAILS
317 This module works through the effect that a
319 delete $SomePackage::{foo};
321 will remove the C<foo> symbol from C<$SomePackage> for run time lookups
322 (e.g., method calls) but will leave the entry alive to be called by
323 already resolved names in the package itself. C<namespace::clean> will
324 restore and therefor in effect keep all glob slots that aren't C<CODE>.
326 A test file has been added to the perl core to ensure that this behaviour
327 will be stable in future releases.
329 Just for completeness sake, if you want to remove the symbol completely,
330 use C<undef> instead.
334 L<B::Hooks::EndOfScope>
336 =head1 AUTHOR AND COPYRIGHT
338 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
339 Matt S Trout for the inspiration on the whole idea.
343 This program is free software; you can redistribute it and/or modify
344 it under the same terms as perl itself.
349 'Danger! Laws of Thermodynamics may not apply.'