updated README
[p5sagit/namespace-clean.git] / README
CommitLineData
40aef9d6 1NAME
6c0ece9b 2 namespace::clean - Keep imports and functions out of your namespace
40aef9d6 3
4VERSION
44c20e05 5 0.11
40aef9d6 6
7SYNOPSIS
8 package Foo;
9 use warnings;
10 use strict;
11
6c0ece9b 12 use Carp qw(croak); # 'croak' will be removed
40aef9d6 13
6c0ece9b 14 sub bar { 23 } # 'bar' will be removed
40aef9d6 15
6c0ece9b 16 # remove all previously defined functions
40aef9d6 17 use namespace::clean;
18
6c0ece9b 19 sub baz { bar() } # 'baz' still defined, 'bar' still bound
40aef9d6 20
6c0ece9b 21 # begin to collection function names from here again
9b680ffe 22 no namespace::clean;
23
6c0ece9b 24 sub quux { baz() } # 'quux' will be removed
9b680ffe 25
6c0ece9b 26 # remove all functions defined after the 'no' unimport
9b680ffe 27 use namespace::clean;
28
6c0ece9b 29 # Will print: 'No', 'No', 'Yes' and 'No'
40aef9d6 30 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
31 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
32 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
9b680ffe 33 print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
40aef9d6 34
35 1;
36
37DESCRIPTION
de673bbf 38 Keeping packages clean
40aef9d6 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
6c0ece9b 42 classes that are included via multiple inheritance by loading them as
43 base classes.
40aef9d6 44
45 The "namespace::clean" pragma will remove all previously declared or
6c0ece9b 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.
49
50 By unimporting via "no" you can tell "namespace::clean" to start
51 collecting functions for the next "use namespace::clean;" specification.
40aef9d6 52
53e92ec5 53 You can use the "-except" flag to tell "namespace::clean" that you don't
472d4b1e 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:
53e92ec5 56
57 use ModuleExportingImport;
58 use namespace::clean -except => [qw( import )];
59
472d4b1e 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.
62
de673bbf 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
66 example:
67
68 package Foo;
69 use strict;
70
71 # blessed NOT available
72
73 sub my_class {
74 use Scalar::Util qw( blessed );
75 use namespace::clean qw( blessed );
76
77 # blessed available
78 return blessed shift;
79 }
80
81 # blessed NOT available
82
1a1be5dc 83 Moose
84 When using "namespace::clean" together with Moose you want to keep the
85 installed "meta" method. So your classes should look like:
86
87 package Foo;
88 use Moose;
89 use namespace::clean -except => 'meta';
90 ...
91
92 Same goes for Moose::Role.
93
44c20e05 94 Cleaning other packages
95 You can tell "namespace::clean" that you want to clean up another
96 package instead of the one importing. To do this you have to pass in the
97 "-cleanee" option like this:
98
99 package My::MooseX::namespace::clean;
100 use strict;
101
102 use namespace::clean (); # no cleanup, just load
103
104 sub import {
105 namespace::clean->import(
106 -cleanee => scalar(caller),
107 -except => 'meta',
108 );
109 }
110
111 If you don't care about "namespace::clean"s discover-and-"-except"
112 logic, and just want to remove subroutines, try "clean_subroutines".
113
40aef9d6 114METHODS
115 You shouldn't need to call any of these. Just "use" the package at the
116 appropriate place.
117
44c20e05 118 clean_subroutines
119 This exposes the actual subroutine-removal logic.
120
121 namespace::clean->clean_subroutines($cleanee, qw( subA subB ));
122
123 will remove "subA" and "subB" from $cleanee. Note that this will remove
124 the subroutines immediately and not wait for scope end. If you want to
125 have this effect at a specific time (e.g. "namespace::clean" acts on
126 scope compile end) it is your responsibility to make sure it runs at
127 that time.
128
40aef9d6 129 import
1a1be5dc 130 Makes a snapshot of the current defined functions and installs a
9cf9958a 131 B::Hooks::EndOfScope hook in the current scope to invoke the cleanups.
40aef9d6 132
9b680ffe 133 unimport
134 This method will be called when you do a
135
136 no namespace::clean;
137
138 It will start a new section of code that defines functions to clean up.
139
140 get_class_store
6c0ece9b 141 This returns a reference to a hash in a passed package containing
9b680ffe 142 information about function names included and excluded from removal.
143
40aef9d6 144 get_functions
145 Takes a class as argument and returns all currently defined functions in
146 it as a hash reference with the function name as key and a typeglob
147 reference to the symbol as value.
148
6c0ece9b 149IMPLEMENTATION DETAILS
150 This module works through the effect that a
151
152 delete $SomePackage::{foo};
153
154 will remove the "foo" symbol from $SomePackage for run time lookups
155 (e.g., method calls) but will leave the entry alive to be called by
472d4b1e 156 already resolved names in the package itself. "namespace::clean" will
157 restore and therefor in effect keep all glob slots that aren't "CODE".
6c0ece9b 158
159 A test file has been added to the perl core to ensure that this
160 behaviour will be stable in future releases.
161
162 Just for completeness sake, if you want to remove the symbol completely,
163 use "undef" instead.
164
40aef9d6 165SEE ALSO
9cf9958a 166 B::Hooks::EndOfScope
40aef9d6 167
168AUTHOR AND COPYRIGHT
169 Robert 'phaylon' Sedlacek "<rs@474.at>", with many thanks to Matt S
170 Trout for the inspiration on the whole idea.
171
172LICENSE
173 This program is free software; you can redistribute it and/or modify it
174 under the same terms as perl itself.
175