15

An often seen recommendation on best practices regarding Moose is the following:

"The use namespace::autoclean bit is simply good code hygiene, as it removes imported symbols from your class's namespace at the end of your package's compile cycle, including Moose keywords. Once the class has been built, these keywords are not needed. (This is preferred to placing no Moose at the end of your package)."

Taken for Moose::Manual::BestPractices (emphasis mine).

I understand everything regarding the quote above, except for one thing: why is the use of namespace::autoclean preferred to the use of no Moose at the end of the lexical scope?

Is it just because namespace::autoclean is more versatile, allowing for more options in its use, or is there something intrinsic to the way it was implemented (specially tailored for Moose classes, perhaps) that makes it more reliable?

I found no documentation clarifying this, and I would much like to know the right answer.

1 Answers1

17

no Moose gets rid of only those functions imported by use Moose. use namespace::autoclean gets rid of all imported functions.

If you always use namespace::autoclean, then you don't have to remember to switch no Moose to use namespace::autoclean if you later come back and import a function from some module. Otherwise, you might forget to do that.

cjm
  • 61,471
  • 9
  • 126
  • 175
  • 10
    `namespace::autoclean` will also break your code if you're planning on using overloads at the same time. For that case you may want to try out `namespace::sweep`. – friedo Mar 05 '12 at 00:56
  • 2
    Current `namespace::autoclean` (v0.28, Oct 2015) documentation [states](http://stackoverflow.com/questions/9559064/why-is-use-namespaceautoclean-preferred-to-no-moose) that "the magic subs installed by overload will not be cleaned". – Davor Cubranic May 24 '16 at 04:05