1namespace::sweep(3) User Contributed Perl Documentation namespace::sweep(3)
2
3
4
6 namespace::sweep - Sweep up imported subs in your classes
7
9 version 0.006
10
12 package Foo;
13
14 use namespace::sweep;
15 use Some::Module qw(some_function);
16
17 sub my_method {
18 my $foo = some_function();
19 ...
20 }
21
22 package main;
23
24 Foo->my_method; # ok
25 Foo->some_function; # ERROR!
26
28 Because Perl methods are just regular subroutines, it's difficult to
29 tell what's a method and what's just an imported function. As a result,
30 imported functions can be called as methods on your objects. This
31 pragma will delete imported functions from your class's symbol table,
32 thereby ensuring that your interface is as you specified it. However,
33 code inside your module will still be able to use the imported
34 functions without any problems.
35
37 The following arguments may be passed on the "use" line:
38
39 -cleanee
40 If you want to clean a different class than the one importing this
41 pragma, you can specify it with this flag. Otherwise, the importing
42 class is assumed.
43
44 package Foo;
45 use namespace::sweep -cleanee => 'Bar' # sweep up Bar.pm
46
47 -also
48 This lets you provide a mechanism to specify other subs to sweep up
49 that would not normally be caught. (For example, private helper
50 subs in your module's class that should not be called as methods.)
51
52 package Foo;
53 use namespace::sweep -also => '_helper'; # sweep up single sub
54 use namespace::sweep -also => [qw/foo bar baz/]; # list of subs
55 use namespace::sweep -also => qr/^secret_/; # subs matching regex
56
57 You can also specify a subroutine reference which will receive the
58 symbol name as $_. If the sub returns true, the symbol will be
59 swept.
60
61 # sweep up those rude four-letter subs
62 use namespace::sweep -also => sub { return 1 if length $_ == 4 }
63
64 You can also combine these methods into an array reference:
65
66 use namespace::sweep -also => [ 'string', sub { 1 if /$pat/ and $_ !~ /$other/ }, qr/^foo_.+/ ];
67
69 This pragma was written to address some problems with the excellent
70 namespace::autoclean. In particular, namespace::autoclean will remove
71 special symbols that are installed by overload, so you can't use
72 namespace::autoclean on objects that overload Perl operators.
73
74 Additionally, namespace::autoclean relies on Class::MOP to figure out
75 the list of methods provided by your class. This pragma does not depend
76 on Class::MOP or Moose, so you can use it for non-Moose classes without
77 worrying about heavy dependencies.
78
79 However, if your class has a Moose (or Moose-compatible) "meta" object,
80 then that will be used to find e.g. methods from composed roles that
81 should not be deleted.
82
83 In most cases, namespace::sweep should work as a drop-in replacement
84 for namespace::autoclean. Upon release, this pragma passes all of
85 namespace::autoclean's tests, in addition to its own.
86
88 This is an early release and there are bound to be a few hiccups along
89 the way.
90
92 Thanks Florian Ragwitz and Tomas Doran for writing and maintaining
93 namespace::autoclean.
94
95 Thanks to Toby Inkster for submitting some better code for finding
96 "meta" objects.
97
99 namespace::autoclean, namespace::clean, overload
100
102 Mike Friedman <friedo@friedo.com>
103
105 This software is copyright (c) 2011 by Mike Friedman.
106
107 This is free software; you can redistribute it and/or modify it under
108 the same terms as the Perl 5 programming language system itself.
109
110
111
112perl v5.30.1 2020-01-30 namespace::sweep(3)