The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


namespace::sweep - Sweep up imported subs in your classes


version 0.006


    package Foo;

    use namespace::sweep;
    use Some::Module qw(some_function);

    sub my_method { 
         my $foo = some_function();

    package main;

    Foo->my_method;      # ok
    Foo->some_function;  # ERROR!


Because Perl methods are just regular subroutines, it's difficult to tell what's a method and what's just an imported function. As a result, imported functions can be called as methods on your objects. This pragma will delete imported functions from your class's symbol table, thereby ensuring that your interface is as you specified it. However, code inside your module will still be able to use the imported functions without any problems.


The following arguments may be passed on the use line:


If you want to clean a different class than the one importing this pragma, you can specify it with this flag. Otherwise, the importing class is assumed.

    package Foo;
    use namespace::sweep -cleanee => 'Bar'   # sweep up

This lets you provide a mechanism to specify other subs to sweep up that would not normally be caught. (For example, private helper subs in your module's class that should not be called as methods.)

    package Foo;
    use namespace::sweep -also => '_helper';          # sweep up single sub
    use namespace::sweep -also => [qw/foo bar baz/];  # list of subs
    use namespace::sweep -also => qr/^secret_/;       # subs matching regex

You can also specify a subroutine reference which will receive the symbol name as $_. If the sub returns true, the symbol will be swept.

    # sweep up those rude four-letter subs
    use namespace::sweep -also => sub { return 1 if length $_ == 4 }

You can also combine these methods into an array reference:

    use namespace::sweep -also => [ 'string', sub { 1 if /$pat/ and $_ !~ /$other/ }, qr/^foo_.+/ ];


This pragma was written to address some problems with the excellent namespace::autoclean. In particular, namespace::autoclean will remove special symbols that are installed by overload, so you can't use namespace::autoclean on objects that overload Perl operators.

Additionally, namespace::autoclean relies on Class::MOP to figure out the list of methods provided by your class. This pragma does not depend on Class::MOP or Moose, so you can use it for non-Moose classes without worrying about heavy dependencies.

However, if your class has a Moose (or Moose-compatible) meta object, then that will be used to find e.g. methods from composed roles that should not be deleted.

In most cases, namespace::sweep should work as a drop-in replacement for namespace::autoclean. Upon release, this pragma passes all of namespace::autoclean's tests, in addition to its own.


This is an early release and there are bound to be a few hiccups along the way.


Thanks Florian Ragwitz and Tomas Doran for writing and maintaining namespace::autoclean.

Thanks to Toby Inkster for submitting some better code for finding meta objects.


namespace::autoclean, namespace::clean, overload


Mike Friedman <>


This software is copyright (c) 2011 by Mike Friedman.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.