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   Explicitely removing functions when your scope is compiled
67       It is also possible to explicitely tell "namespace::clean" what
68       packages to remove when the surrounding scope has finished compiling.
69       Here is an example:
70
71         package Foo;
72         use strict;
73
74         # blessed NOT available
75
76         sub my_class {
77             use Scalar::Util qw( blessed );
78             use namespace::clean qw( blessed );
79
80             # blessed available
81             return blessed shift;
82         }
83
84         # blessed NOT available
85
86   Moose
87       When using "namespace::clean" together with Moose you want to keep the
88       installed "meta" method. So your classes should look like:
89
90         package Foo;
91         use Moose;
92         use namespace::clean -except => 'meta';
93         ...
94
95       Same goes for Moose::Role.
96
97   Cleaning other packages
98       You can tell "namespace::clean" that you want to clean up another
99       package instead of the one importing. To do this you have to pass in
100       the "-cleanee" option like this:
101
102         package My::MooseX::namespace::clean;
103         use strict;
104
105         use namespace::clean (); # no cleanup, just load
106
107         sub import {
108             namespace::clean->import(
109               -cleanee => scalar(caller),
110               -except  => 'meta',
111             );
112         }
113
114       If you don't care about "namespace::clean"s discover-and-"-except"
115       logic, and just want to remove subroutines, try "clean_subroutines".
116

METHODS

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
129   import
130       Makes a snapshot of the current defined functions and installs a
131       B::Hooks::EndOfScope hook in the current scope to invoke the cleanups.
132
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
141       This returns a reference to a hash in a passed package containing
142       information about function names included and excluded from removal.
143
144   get_functions
145       Takes a class as argument and returns all currently defined functions
146       in it as a hash reference with the function name as key and a typeglob
147       reference to the symbol as value.
148

IMPLEMENTATION 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
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".
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
163       completely, use "undef" instead.
164

SEE ALSO

166       B::Hooks::EndOfScope
167

THANKS

169       Many thanks to Matt S Trout for the inspiration on the whole idea.
170

AUTHORS

172       ·   Robert 'phaylon' Sedlacek <rs@474.at>
173
174       ·   Florian Ragwitz <rafl@debian.org>
175
176       ·   Jesse Luehrs <doy@tozt.net>
177
179       This software is copyright (c) 2010 by Robert 'phaylon' Sedlacek.
180
181       This is free software; you can redistribute it and/or modify it under
182       the same terms as the Perl 5 programming language system itself.
183
184
185
186perl v5.12.1                      2010-06-13               namespace::clean(3)
Impressum