implemented -cleanee option plus an external callable remove_subroutines class method...
[p5sagit/namespace-clean.git] / lib / namespace / clean.pm
CommitLineData
40aef9d6 1package namespace::clean;
2
3=head1 NAME
4
6c0ece9b 5namespace::clean - Keep imports and functions out of your namespace
40aef9d6 6
7=cut
8
9use warnings;
10use strict;
11
de673bbf 12use vars qw( $VERSION $STORAGE_VAR $SCOPE_HOOK_KEY $SCOPE_EXPLICIT );
1a1be5dc 13use Symbol qw( qualify_to_ref );
aa2aafae 14use B::Hooks::EndOfScope;
40aef9d6 15
16=head1 VERSION
17
fcfe7810 180.11
40aef9d6 19
20=cut
21
fcfe7810 22$VERSION = '0.11';
de673bbf 23$STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
40aef9d6 24
25=head1 SYNOPSIS
26
27 package Foo;
28 use warnings;
29 use strict;
30
6c0ece9b 31 use Carp qw(croak); # 'croak' will be removed
40aef9d6 32
6c0ece9b 33 sub bar { 23 } # 'bar' will be removed
40aef9d6 34
6c0ece9b 35 # remove all previously defined functions
40aef9d6 36 use namespace::clean;
37
6c0ece9b 38 sub baz { bar() } # 'baz' still defined, 'bar' still bound
40aef9d6 39
6c0ece9b 40 # begin to collection function names from here again
9b680ffe 41 no namespace::clean;
42
6c0ece9b 43 sub quux { baz() } # 'quux' will be removed
9b680ffe 44
6c0ece9b 45 # remove all functions defined after the 'no' unimport
9b680ffe 46 use namespace::clean;
47
6c0ece9b 48 # Will print: 'No', 'No', 'Yes' and 'No'
40aef9d6 49 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
50 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
51 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
9b680ffe 52 print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
40aef9d6 53
54 1;
55
56=head1 DESCRIPTION
57
de673bbf 58=head2 Keeping packages clean
59
40aef9d6 60When you define a function, or import one, into a Perl package, it will
61naturally also be available as a method. This does not per se cause
62problems, but it can complicate subclassing and, for example, plugin
6c0ece9b 63classes that are included via multiple inheritance by loading them as
64base classes.
40aef9d6 65
66The C<namespace::clean> pragma will remove all previously declared or
67imported symbols at the end of the current package's compile cycle.
6c0ece9b 68Functions called in the package itself will still be bound by their
69name, but they won't show up as methods on your class or instances.
70
71By unimporting via C<no> you can tell C<namespace::clean> to start
72collecting functions for the next C<use namespace::clean;> specification.
40aef9d6 73
53e92ec5 74You can use the C<-except> flag to tell C<namespace::clean> that you
472d4b1e 75don't want it to remove a certain function or method. A common use would
76be a module exporting an C<import> method along with some functions:
53e92ec5 77
78 use ModuleExportingImport;
79 use namespace::clean -except => [qw( import )];
80
472d4b1e 81If you just want to C<-except> a single sub, you can pass it directly.
82For more than one value you have to use an array reference.
83
de673bbf 84=head2 Explicitely removing functions when your scope is compiled
85
86It is also possible to explicitely tell C<namespace::clean> what packages
87to remove when the surrounding scope has finished compiling. Here is an
88example:
89
90 package Foo;
91 use strict;
92
93 # blessed NOT available
94
95 sub my_class {
96 use Scalar::Util qw( blessed );
97 use namespace::clean qw( blessed );
98
99 # blessed available
100 return blessed shift;
101 }
102
103 # blessed NOT available
104
1a1be5dc 105=head2 Moose
106
107When using C<namespace::clean> together with L<Moose> you want to keep
108the installed C<meta> method. So your classes should look like:
109
110 package Foo;
111 use Moose;
112 use namespace::clean -except => 'meta';
113 ...
114
115Same goes for L<Moose::Role>.
116
fcfe7810 117=head2 Cleaning other packages
118
119You can tell C<namespace::clean> that you want to clean up another package
120instead of the one importing. To do this you have to pass in the C<-cleanee>
121option like this:
122
123 package My::MooseX::namespace::clean;
124 use strict;
125
126 use namespace::clean (); # no cleanup, just load
127
128 sub import {
129 namespace::clean->import(
130 -cleanee => scalar(caller),
131 -except => 'meta',
132 );
133 }
134
135If you don't care about C<namespace::clean>s discover-and-C<-except> logic, and
136just want to remove subroutines, try L</clean_subroutines>.
137
40aef9d6 138=head1 METHODS
139
140You shouldn't need to call any of these. Just C<use> the package at the
141appropriate place.
142
6c0ece9b 143=cut
144
fcfe7810 145=head2 clean_subroutines
40aef9d6 146
fcfe7810 147This exposes the actual subroutine-removal logic.
148
149 namespace::clean->clean_subroutines($cleanee, qw( subA subB ));
150
151will remove C<subA> and C<subB> from C<$cleanee>. Note that this will remove the
152subroutines B<immediately> and not wait for scope end. If you want to have this
153effect at a specific time (e.g. C<namespace::clean> acts on scope compile end)
154it is your responsibility to make sure it runs at that time.
40aef9d6 155
156=cut
157
de673bbf 158my $RemoveSubs = sub {
fcfe7810 159
de673bbf 160 my $cleanee = shift;
161 my $store = shift;
162 SYMBOL:
163 for my $f (@_) {
164
165 # ignore already removed symbols
166 next SYMBOL if $store->{exclude}{ $f };
167 no strict 'refs';
168
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 };
173 }
174
175 SLOT:
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 };
180 }
181 }
182};
53e92ec5 183
fcfe7810 184sub clean_subroutines {
185 my ($nc, $cleanee, @subs) = @_;
186 $RemoveSubs->($cleanee, {}, @subs);
187}
188
189=head2 import
190
191Makes a snapshot of the current defined functions and installs a
192L<B::Hooks::EndOfScope> hook in the current scope to invoke the cleanups.
193
194=cut
195
de673bbf 196sub import {
197 my ($pragma, @args) = @_;
53e92ec5 198
de673bbf 199 my (%args, $is_explicit);
fcfe7810 200
201 ARG:
202 while (@args) {
203
204 if ($args[0] =~ /^\-/) {
205 my $key = shift @args;
206 my $value = shift @args;
207 $args{ $key } = $value;
208 }
209 else {
210 $is_explicit++;
211 last ARG;
212 }
9b680ffe 213 }
214
fcfe7810 215 my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller;
de673bbf 216 if ($is_explicit) {
aa2aafae 217 on_scope_end {
de673bbf 218 $RemoveSubs->($cleanee, {}, @args);
aa2aafae 219 };
9b680ffe 220 }
de673bbf 221 else {
222
223 # calling class, all current functions and our storage
224 my $functions = $pragma->get_functions($cleanee);
225 my $store = $pragma->get_class_store($cleanee);
226
227 # except parameter can be array ref or single value
228 my %except = map {( $_ => 1 )} (
229 $args{ -except }
230 ? ( ref $args{ -except } eq 'ARRAY' ? @{ $args{ -except } } : $args{ -except } )
231 : ()
232 );
233
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;
240 }
241
242 # register EOF handler on first call to import
243 unless ($store->{handler_is_installed}) {
aa2aafae 244 on_scope_end {
de673bbf 245 $RemoveSubs->($cleanee, $store, keys %{ $store->{remove} });
aa2aafae 246 };
de673bbf 247 $store->{handler_is_installed} = 1;
248 }
249
250 return 1;
251 }
9b680ffe 252}
253
254=head2 unimport
255
256This method will be called when you do a
257
258 no namespace::clean;
259
260It will start a new section of code that defines functions to clean up.
261
262=cut
263
264sub unimport {
fcfe7810 265 my ($pragma, %args) = @_;
9b680ffe 266
6c0ece9b 267 # the calling class, the current functions and our storage
fcfe7810 268 my $cleanee = exists $args{ -cleanee } ? $args{ -cleanee } : scalar caller;
9b680ffe 269 my $functions = $pragma->get_functions($cleanee);
270 my $store = $pragma->get_class_store($cleanee);
271
6c0ece9b 272 # register all unknown previous functions as excluded
9b680ffe 273 for my $f (keys %$functions) {
274 next if $store->{remove}{ $f }
275 or $store->{exclude}{ $f };
276 $store->{exclude}{ $f } = 1;
277 }
278
279 return 1;
280}
281
282=head2 get_class_store
283
6c0ece9b 284This returns a reference to a hash in a passed package containing
285information about function names included and excluded from removal.
9b680ffe 286
287=cut
288
289sub get_class_store {
290 my ($pragma, $class) = @_;
291 no strict 'refs';
292 return \%{ "${class}::${STORAGE_VAR}" };
40aef9d6 293}
294
295=head2 get_functions
296
297Takes a class as argument and returns all currently defined functions
298in it as a hash reference with the function name as key and a typeglob
299reference to the symbol as value.
300
301=cut
302
303sub get_functions {
304 my ($pragma, $class) = @_;
305
306 return {
6c0ece9b 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
40aef9d6 312 };
313}
314
6c0ece9b 315=head1 IMPLEMENTATION DETAILS
316
317This module works through the effect that a
318
319 delete $SomePackage::{foo};
320
321will 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
472d4b1e 323already resolved names in the package itself. C<namespace::clean> will
324restore and therefor in effect keep all glob slots that aren't C<CODE>.
6c0ece9b 325
326A test file has been added to the perl core to ensure that this behaviour
327will be stable in future releases.
328
329Just for completeness sake, if you want to remove the symbol completely,
330use C<undef> instead.
331
40aef9d6 332=head1 SEE ALSO
333
705fe1b1 334L<B::Hooks::EndOfScope>
40aef9d6 335
336=head1 AUTHOR AND COPYRIGHT
337
338Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
339Matt S Trout for the inspiration on the whole idea.
340
341=head1 LICENSE
342
343This program is free software; you can redistribute it and/or modify
344it under the same terms as perl itself.
345
346=cut
347
de673bbf 348no warnings;
349'Danger! Laws of Thermodynamics may not apply.'