Announcing namespace::sweep

A question recently appeared on Stack Overflow about using namespace::autoclean in conjunction with operator overloading.

It turns out that you can't, because namespace::autoclean, in addition to cleaning up symbols for imported subs, also cleans up the operator magic installed by overload.pm.

This had been noticed before, but the suggested fix was rejected in favor of implementing the logic to support overload methods in Class::MOP, which namespace::autoclean uses under the hood to figure out the list of allowed methods for a class, and it doesn't look like anyone is interested in doing that right now.

As it happens, I'm working on a distribution right now which uses Moose and does some operator overloading, so namespace::autoclean would be a problem. I decided that there's really no reason a symbol-cleaning pragma should depend on Moose (or the MOP) so I wrote namespace::sweep as a replacement. More on the advantages and disadvantages below.

Advantages of namespace::sweep

  1. As mentioned above, it won't clobber your overloads.
  2. namespace::sweep provides the same interface as namespace::autoclean, so you should be able to use it as a drop-in replacement.
  3. namespace::sweep passes all of namespace::autoclean's tests, in addition to its own. So there's a reasonable chance that it actually works.
  4. There is no dependance on MOP or Moose, so you can use it for classes built on other frameworks or for which you do not want bulky dependencies.
  5. However, if your class has a Moose (or Moose-compatible) meta object, that will be used to determine the list of methods to keep. Therefore methods composed via roles will Just Work.
  6. The name has the potential for Mr. Miyagi jokes.

Disadvantages of namespace::sweep

  1. It's new. There are probably problems with it. I haven't used it in anything yet.
  2. It won't work for non-Moose-ish things that add methods to your class, like Mixin.pm or CGI::Application plugins. I have a plan to add a feature that will allow you to exclude symbols from certain classes from being swept, but I haven't written it yet.
  3. Although the dependencies are lighter than namespace::autoclean, it still depends on XS modules including B::Hooks::EndOfScope and Sub::Identify, which can make installation difficult on some platforms.

I look forward to hearing your thoughts on this. I'm going to start using it immediately for the project I mentioned above, and I'm sure I will uncover some bugs along the way. The repository is here.

3 Comments

Seriously well done!

+1. Thank you, I plan to use this.

Great idea! Now I can sweep...you hold like this

Leave a comment

About Mike Friedman

user-pic Mike Friedman is a professional computer programmer living and working in the New York City area.