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