1namespace::autoclean(3)User Contributed Perl Documentationnamespace::autoclean(3)
2
3
4
6 namespace::autoclean - Keep imports out of your namespace
7
9 version 0.29
10
12 package Foo;
13 use namespace::autoclean;
14 use Some::Package qw/imported_function/;
15
16 sub bar { imported_function('stuff') }
17
18 # later on:
19 Foo->bar; # works
20 Foo->imported_function; # will fail. imported_function got cleaned after compilation
21
23 When you import a function into a Perl package, it will naturally also
24 be available as a method.
25
26 The "namespace::autoclean" pragma will remove all imported symbols at
27 the end of the current package's compile cycle. Functions called in the
28 package itself will still be bound by their name, but they won't show
29 up as methods on your class or instances.
30
31 This module is very similar to namespace::clean, except it will clean
32 all imported functions, no matter if you imported them before or after
33 you "use"d the pragma. It will also not touch anything that looks like
34 a method.
35
36 If you're writing an exporter and you want to clean up after yourself
37 (and your peers), you can use the "-cleanee" switch to specify what
38 package to clean:
39
40 package My::MooseX::namespace::autoclean;
41 use strict;
42
43 use namespace::autoclean (); # no cleanup, just load
44
45 sub import {
46 namespace::autoclean->import(
47 -cleanee => scalar(caller),
48 );
49 }
50
52 "namespace::autoclean" will leave behind anything that it deems a
53 method. For Moose classes, this the based on the "get_method_list"
54 method on from the Class::MOP::Class. For non-Moose classes, anything
55 defined within the package will be identified as a method. This should
56 match Moose's definition of a method. Additionally, the magic subs
57 installed by overload will not be cleaned.
58
60 -also => [ ITEM | REGEX | SUB, .. ]
61 -also => ITEM
62 -also => REGEX
63 -also => SUB
64 Sometimes you don't want to clean imports only, but also helper
65 functions you're using in your methods. The "-also" switch can be used
66 to declare a list of functions that should be removed additional to any
67 imports:
68
69 use namespace::autoclean -also => ['some_function', 'another_function'];
70
71 If only one function needs to be additionally cleaned the "-also"
72 switch also accepts a plain string:
73
74 use namespace::autoclean -also => 'some_function';
75
76 In some situations, you may wish for a more powerful cleaning solution.
77
78 The "-also" switch can take a Regex or a CodeRef to match against local
79 function names to clean.
80
81 use namespace::autoclean -also => qr/^_/
82
83 use namespace::autoclean -also => sub { $_ =~ m{^_} };
84
85 use namespace::autoclean -also => [qr/^_/ , qr/^hidden_/ ];
86
87 use namespace::autoclean -also => [sub { $_ =~ m/^_/ or $_ =~ m/^hidden/ }, sub { uc($_) == $_ } ];
88
89 -except => [ ITEM | REGEX | SUB, .. ]
90 -except => ITEM
91 -except => REGEX
92 -except => SUB
93 This takes exactly the same options as "-also" except that anything
94 this matches will not be cleaned.
95
97 When used with Moo classes, the heuristic used to check for methods
98 won't work correctly for methods from roles consumed at compile time.
99
100 package My::Class;
101 use Moo;
102 use namespace::autoclean;
103
104 # Bad, any consumed methods will be cleaned
105 BEGIN { with 'Some::Role' }
106
107 # Good, methods from role will be maintained
108 with 'Some::Role';
109
110 Additionally, method detection may not work properly in Mouse classes
111 in perls earlier than 5.10.
112
114 • namespace::clean
115
116 • B::Hooks::EndOfScope
117
118 • namespace::sweep
119
120 • Sub::Exporter::ForMethods
121
122 • Sub::Name
123
124 • Sub::Install
125
126 • Test::CleanNamespaces
127
128 • Dist::Zilla::Plugin::Test::CleanNamespaces
129
131 Bugs may be submitted through the RT bug tracker
132 <https://rt.cpan.org/Public/Dist/Display.html?Name=namespace-autoclean>
133 (or bug-namespace-autoclean@rt.cpan.org <mailto:bug-namespace-
134 autoclean@rt.cpan.org>).
135
136 There is also a mailing list available for users of this distribution,
137 at <http://lists.perl.org/list/moose.html>.
138
139 There is also an irc channel available for users of this distribution,
140 at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
141
143 Florian Ragwitz <rafl@debian.org>
144
146 • Karen Etheridge <ether@cpan.org>
147
148 • Graham Knop <haarg@haarg.org>
149
150 • Dave Rolsky <autarch@urth.org>
151
152 • Kent Fredric <kentfredric@gmail.com>
153
154 • Tomas Doran <bobtfish@bobtfish.net>
155
156 • Shawn M Moore <cpan@sartak.org>
157
158 • Felix Ostmann <sadrak@cpan.org>
159
160 • Chris Prather <chris@prather.org>
161
162 • Andrew Rodland <arodland@cpan.org>
163
165 This software is copyright (c) 2009 by Florian Ragwitz.
166
167 This is free software; you can redistribute it and/or modify it under
168 the same terms as the Perl 5 programming language system itself.
169
170
171
172perl v5.34.0 2022-01-21 namespace::autoclean(3)