2 namespace::clean - Keep imports and functions out of your namespace
12 use Carp qw(croak); # 'croak' will be removed
14 sub bar { 23 } # 'bar' will be removed
16 # remove all previously defined functions
19 sub baz { bar() } # 'baz' still defined, 'bar' still bound
21 # begin to collection function names from here again
24 sub quux { baz() } # 'quux' will be removed
26 # remove all functions defined after the 'no' unimport
29 # Will print: 'No', 'No', 'Yes' and 'No'
30 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
31 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
32 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
33 print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
38 Keeping packages clean
39 When you define a function, or import one, into a Perl package, it will
40 naturally also be available as a method. This does not per se cause
41 problems, but it can complicate subclassing and, for example, plugin
42 classes that are included via multiple inheritance by loading them as
45 The "namespace::clean" pragma will remove all previously declared or
46 imported symbols at the end of the current package's compile cycle.
47 Functions called in the package itself will still be bound by their
48 name, but they won't show up as methods on your class or instances.
50 By unimporting via "no" you can tell "namespace::clean" to start
51 collecting functions for the next "use namespace::clean;" specification.
53 You can use the "-except" flag to tell "namespace::clean" that you don't
54 want it to remove a certain function or method. A common use would be a
55 module exporting an "import" method along with some functions:
57 use ModuleExportingImport;
58 use namespace::clean -except => [qw( import )];
60 If you just want to "-except" a single sub, you can pass it directly.
61 For more than one value you have to use an array reference.
63 Explicitely removing functions when your scope is compiled
64 It is also possible to explicitely tell "namespace::clean" what packages
65 to remove when the surrounding scope has finished compiling. Here is an
71 # blessed NOT available
74 use Scalar::Util qw( blessed );
75 use namespace::clean qw( blessed );
81 # blessed NOT available
84 When using "namespace::clean" together with Moose you want to keep the
85 installed "meta" method. So your classes should look like:
89 use namespace::clean -except => 'meta';
92 Same goes for Moose::Role.
95 You shouldn't need to call any of these. Just "use" the package at the
99 Makes a snapshot of the current defined functions and installs a
100 Scope::Guard in the current scope to invoke the cleanups.
103 This method will be called when you do a
107 It will start a new section of code that defines functions to clean up.
110 This returns a reference to a hash in a passed package containing
111 information about function names included and excluded from removal.
114 Takes a class as argument and returns all currently defined functions in
115 it as a hash reference with the function name as key and a typeglob
116 reference to the symbol as value.
118 IMPLEMENTATION DETAILS
119 This module works through the effect that a
121 delete $SomePackage::{foo};
123 will remove the "foo" symbol from $SomePackage for run time lookups
124 (e.g., method calls) but will leave the entry alive to be called by
125 already resolved names in the package itself. "namespace::clean" will
126 restore and therefor in effect keep all glob slots that aren't "CODE".
128 A test file has been added to the perl core to ensure that this
129 behaviour will be stable in future releases.
131 Just for completeness sake, if you want to remove the symbol completely,
138 Robert 'phaylon' Sedlacek "<rs@474.at>", with many thanks to Matt S
139 Trout for the inspiration on the whole idea.
142 This program is free software; you can redistribute it and/or modify it
143 under the same terms as perl itself.