Use or not to use the namespace::sweep and/or Modern::Perl

倾然丶 夕夏残阳落幕 提交于 2019-12-07 14:05:08

问题


In my last question @Borodin commented my question with:

You should start by removing Modern::Perl and namespace::sweep. Modules that behave as pragma should be avoided.

I'm confused a bit, because:

  • in the LATEST Moose BestPractices manual recommending to use namespace::autoclean.

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).

In the Book Intermediate perl recommending to use the namespace::autoclean too.

Yes, I'm used the instead of the autoclean the sweep module - because again from the doccu

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.

... ...

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.

And because I'm an perl beginner, i'm really confused. For me, when i reading: this module addressing some problems of another module - mean: use this one.

'Manual (where from I should learn) says "use it" and expert from stackoverflow teling: don't use it.

So please, can someone explain me:

  • it is correct to use namespace::sweep or I should use namespace::autoclean or none of them?
  • if none, why the BestPractices recommends it?

For the `ModernPerl'. Sure, I'm probably don't understand deeply and "exactly" what is does. What i know, (again from it's doccu)

This enables the strict and warnings pragmas, as well as all of the features available in Perl 5.10. It also enables C3 method resolution order as documented in perldoc mro and loads IO::File and IO::Handle so that you may call methods on filehandles. In the future, it may include additional core modules and pragmas.

Sure, don't deeply understand to mro, only think it is an answer to the "deadly diamond of death" problem in cases of multiple inheritance.

Up to today, i was really happy with it, because it is shorting for me the needed pragmas:

use strict;
use warnings;
use feature 'say';

So, what is a "status" of the "Modern::Perl" (and other similar cpanm modules)? Allowed to use, or not?


回答1:


On your question about namespace::sweep:

Firstly, take note of the actual problem that namespace::sweep resolves.

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.

What this means is that if your class has overloaded operators they won't work if you also use namespace::autoclean. But this problem only occurs if you use overload. Other than that, namespace::autoclean will suffice.

Secondly, it says that namespace::sweep can be used instead of namespace::autoclean:

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.

So to answer your question, "is it correct to use namespace::sweep or I should use namespace::autoclean or none of them?"

  1. You should use at least one of them as recommended by Moose Best Practices.
  2. It is generally ok to use namespace::sweep since it says it is designed to do so and it passes all of namespace::autoclean's tests.
  3. In spite of point 2 above, if you don't use overload then you don't have a problem with using namespace::autoclean, so you could just use that in this case.



回答2:


Specifying use 5.014; (or other version >= 5.011) will automatically do use strict; and enable all features of that version for you; IMO this is some of the reason Modern::Perl has not gained a whole lot of traction.

I've always disliked Modern::Perl because it doesn't describe what it does; it is someone else's idea of "modern" as of some fixed point in the past.



来源:https://stackoverflow.com/questions/22599829/use-or-not-to-use-the-namespacesweep-and-or-modernperl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!