1namespace::clean(3)   User Contributed Perl Documentation  namespace::clean(3)
2
3
4

NAME

6       namespace::clean - Keep imports and functions out of your namespace
7

SYNOPSIS

9         package Foo;
10         use warnings;
11         use strict;
12
13         use Carp qw(croak);   # 'croak' will be removed
14
15         sub bar { 23 }        # 'bar' will be removed
16
17         # remove all previously defined functions
18         use namespace::clean;
19
20         sub baz { bar() }     # 'baz' still defined, 'bar' still bound
21
22         # begin to collection function names from here again
23         no namespace::clean;
24
25         sub quux { baz() }    # 'quux' will be removed
26
27         # remove all functions defined after the 'no' unimport
28         use namespace::clean;
29
30         # Will print: 'No', 'No', 'Yes' and 'No'
31         print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
32         print +(__PACKAGE__->can('bar')   ? 'Yes' : 'No'), "\n";
33         print +(__PACKAGE__->can('baz')   ? 'Yes' : 'No'), "\n";
34         print +(__PACKAGE__->can('quux')  ? 'Yes' : 'No'), "\n";
35
36         1;
37

DESCRIPTION

39   Keeping packages clean
40       When you define a function, or import one, into a Perl package, it will
41       naturally also be available as a method. This does not per se cause
42       problems, but it can complicate subclassing and, for example, plugin
43       classes that are included via multiple inheritance by loading them as
44       base classes.
45
46       The "namespace::clean" pragma will remove all previously declared or
47       imported symbols at the end of the current package's compile cycle.
48       Functions called in the package itself will still be bound by their
49       name, but they won't show up as methods on your class or instances.
50
51       By unimporting via "no" you can tell "namespace::clean" to start
52       collecting functions for the next "use namespace::clean;"
53       specification.
54
55       You can use the "-except" flag to tell "namespace::clean" that you
56       don't want it to remove a certain function or method. A common use
57       would be a module exporting an "import" method along with some
58       functions:
59
60         use ModuleExportingImport;
61         use namespace::clean -except => [qw( import )];
62
63       If you just want to "-except" a single sub, you can pass it directly.
64       For more than one value you have to use an array reference.
65
66       Late binding caveat
67
68       Note that the technique used by this module relies on perl having
69       resolved all names to actual code references during the compilation of
70       a scope. While this is almost always what the interpreter does, there
71       are some exceptions, notably the sort SUBNAME style of the "sort"
72       built-in invocation. The following example will not work, because
73       "sort" does not try to resolve the function name to an actual code
74       reference until runtime.
75
76        use MyApp::Utils 'my_sorter';
77        use namespace::clean;
78
79        my @sorted = sort my_sorter @list;
80
81       You need to work around this by forcing a compile-time resolution like
82       so:
83
84        use MyApp::Utils 'my_sorter';
85        use namespace::clean;
86
87        my $my_sorter_cref = \&my_sorter;
88
89        my @sorted = sort $my_sorter_cref @list;
90
91   Explicitly removing functions when your scope is compiled
92       It is also possible to explicitly tell "namespace::clean" what packages
93       to remove when the surrounding scope has finished compiling. Here is an
94       example:
95
96         package Foo;
97         use strict;
98
99         # blessed NOT available
100
101         sub my_class {
102             use Scalar::Util qw( blessed );
103             use namespace::clean qw( blessed );
104
105             # blessed available
106             return blessed shift;
107         }
108
109         # blessed NOT available
110
111   Moose
112       When using "namespace::clean" together with Moose you want to keep the
113       installed "meta" method. So your classes should look like:
114
115         package Foo;
116         use Moose;
117         use namespace::clean -except => 'meta';
118         ...
119
120       Same goes for Moose::Role.
121
122   Cleaning other packages
123       You can tell "namespace::clean" that you want to clean up another
124       package instead of the one importing. To do this you have to pass in
125       the "-cleanee" option like this:
126
127         package My::MooseX::namespace::clean;
128         use strict;
129
130         use namespace::clean (); # no cleanup, just load
131
132         sub import {
133             namespace::clean->import(
134               -cleanee => scalar(caller),
135               -except  => 'meta',
136             );
137         }
138
139       If you don't care about "namespace::clean"s discover-and-"-except"
140       logic, and just want to remove subroutines, try "clean_subroutines".
141

METHODS

143   clean_subroutines
144       This exposes the actual subroutine-removal logic.
145
146         namespace::clean->clean_subroutines($cleanee, qw( subA subB ));
147
148       will remove "subA" and "subB" from $cleanee. Note that this will remove
149       the subroutines immediately and not wait for scope end. If you want to
150       have this effect at a specific time (e.g. "namespace::clean" acts on
151       scope compile end) it is your responsibility to make sure it runs at
152       that time.
153
154   import
155       Makes a snapshot of the current defined functions and installs a
156       B::Hooks::EndOfScope hook in the current scope to invoke the cleanups.
157
158   unimport
159       This method will be called when you do a
160
161         no namespace::clean;
162
163       It will start a new section of code that defines functions to clean up.
164
165   get_class_store
166       This returns a reference to a hash in a passed package containing
167       information about function names included and excluded from removal.
168
169   get_functions
170       Takes a class as argument and returns all currently defined functions
171       in it as a hash reference with the function name as key and a typeglob
172       reference to the symbol as value.
173

IMPLEMENTATION DETAILS

175       This module works through the effect that a
176
177         delete $SomePackage::{foo};
178
179       will remove the "foo" symbol from $SomePackage for run time lookups
180       (e.g., method calls) but will leave the entry alive to be called by
181       already resolved names in the package itself. "namespace::clean" will
182       restore and therefor in effect keep all glob slots that aren't "CODE".
183
184       A test file has been added to the perl core to ensure that this
185       behaviour will be stable in future releases.
186
187       Just for completeness sake, if you want to remove the symbol
188       completely, use "undef" instead.
189

SEE ALSO

191       B::Hooks::EndOfScope
192

THANKS

194       Many thanks to Matt S Trout for the inspiration on the whole idea.
195

AUTHORS

197       ·   Robert 'phaylon' Sedlacek <rs@474.at>
198
199       ·   Florian Ragwitz <rafl@debian.org>
200
201       ·   Jesse Luehrs <doy@tozt.net>
202
203       ·   Peter Rabbitson <ribasushi@cpan.org>
204
205       ·   Father Chrysostomos <sprout@cpan.org>
206
208       This software is copyright (c) 2011 by "AUTHORS"
209
210       This is free software; you can redistribute it and/or modify it under
211       the same terms as the Perl 5 programming language system itself.
212
213
214
215perl v5.30.0                      2019-07-26               namespace::clean(3)
Impressum