1namespace::autoclean(3)User Contributed Perl Documentationnamespace::autoclean(3)
2
3
4
6 namespace::autoclean - Keep imports out of your namespace
7
9 package Foo;
10 use namespace::autoclean;
11 use Some::Package qw/imported_function/;
12
13 sub bar { imported_function('stuff') }
14
15 # later on:
16 Foo->bar; # works
17 Foo->imported_function; # will fail. imported_function got cleaned after compilation
18
20 When you import a function into a Perl package, it will naturally also
21 be available as a method.
22
23 The "namespace::autoclean" pragma will remove all imported symbols at
24 the end of the current package's compile cycle. Functions called in the
25 package itself will still be bound by their name, but they won't show
26 up as methods on your class or instances.
27
28 This module is very similar to namespace::clean, except it will clean
29 all imported functions, no matter if you imported them before or after
30 you "use"d the pragma. It will also not touch anything that looks like
31 a method, according to "Class::MOP::Class::get_method_list".
32
33 If you're writing an exporter and you want to clean up after yourself
34 (and your peers), you can use the "-cleanee" switch to specify what
35 package to clean:
36
37 package My::MooseX::namespace::autoclean;
38 use strict;
39
40 use namespace::autoclean (); # no cleanup, just load
41
42 sub import {
43 namespace::autoclean->import(
44 -cleanee => scalar(caller),
45 );
46 }
47
49 -also => [ ITEM | REGEX | SUB, .. ]
50 -also => ITEM
51 -also => REGEX
52 -also => SUB
53 Sometimes you don't want to clean imports only, but also helper
54 functions you're using in your methods. The "-also" switch can be used
55 to declare a list of functions that should be removed additional to any
56 imports:
57
58 use namespace::autoclean -also => ['some_function', 'another_function'];
59
60 If only one function needs to be additionally cleaned the "-also"
61 switch also accepts a plain string:
62
63 use namespace::autoclean -also => 'some_function';
64
65 In some situations, you may wish for a more powerful cleaning solution.
66
67 The "-also" switch can take a Regex or a CodeRef to match against local
68 function names to clean.
69
70 use namespace::autoclean -also => qr/^_/
71
72 use namespace::autoclean -also => sub { $_ =~ m{^_} };
73
74 use namespace::autoclean -also => [qr/^_/ , qr/^hidden_/ ];
75
76 use namespace::autoclean -also => [sub { $_ =~ m/^_/ or $_ =~ m/^hidden/ }, sub { uc($_) == $_ } ];
77
79 namespace::clean
80
81 Class::MOP
82
83 B::Hooks::EndOfScope
84
86 Florian Ragwitz <rafl@debian.org>
87
89 This software is copyright (c) 2010 by Florian Ragwitz.
90
91 This is free software; you can redistribute it and/or modify it under
92 the same terms as the Perl 5 programming language system itself.
93
94
95
96perl v5.12.0 2010-05-07 namespace::autoclean(3)