如何将 Perl 代码标记为已弃用?

发布于 2024-08-11 07:01:21 字数 523 浏览 2 评论 0原文

在我的项目中,我目前正在准备从遗留代码逐步迁移到经过正确设计和测试的新模块。由于并非每个程序员同事都密切关注我所做的事情,因此我想在使用旧代码时发出警告。我也非常希望能够输出有关如何移植旧代码的建议。

我找到了两种方法:

  1. Attribute::Deprecated ,这对于函数来说很好,但如果不推荐使用完整的模块,则相当麻烦。此外,除了警告之外,没有其他信息。

  2. 用于模块的

    Perl::Critic::Policy::Modules::ProhibitEvilModules 或自定义的 Perl::Critic 规则,以便在函数或方法级别上进行更精细的弃用。此方法很好,但从代码本身来看,它已被弃用并不能立即明显看出。

还有其他建议或技巧如何正确且轻松地做到这一点吗?

In my project I'm currently preparing a step-by-step move from legacy code to new, properly-designed and tested modules. Since not every fellow programmer follows closely what I do, I would like to emit warnings when old code is used. I would also strongly prefer being able to output recommendations on how to port old code.

I've found two ways of doing it:

  1. Attribute::Deprecated, which is fine for functions, but rather cumbersome if a complete module is deprecated. Also, no additional information apart from warnings.

  2. Perl::Critic::Policy::Modules::ProhibitEvilModules for modules or maybe a custom Perl::Critic rule for finer deprecation on function or method level. This method is fine, but it's not immediately obvious from code itself that it's deprecated.

Any other suggestions or tricks how to do this properly and easy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2024-08-18 07:01:21

对于方法和函数,您只需用警告和对首选函数的调用替换函数体即可。

perl perllexwarn 给出以下示例:

 package MyMod::Abc;

 sub open {
     warnings::warnif("deprecated",
     "open is deprecated, use new instead");
     new(@_);
 }

 sub new {
     # ...
 }
 1;

如果您要弃用整个模块,将警告放在模块的 BEGIN 块中。

您还可以将警告放入 import 方法中(例如 Win32::GUI::import):这完全取决于您想要做什么。

For methods and functions, you can just replace the body of the function with a warning and a call to the preferred function.

perl perllexwarn gives the following example:

 package MyMod::Abc;

 sub open {
     warnings::warnif("deprecated",
     "open is deprecated, use new instead");
     new(@_);
 }

 sub new {
     # ...
 }
 1;

If you are deprecating a whole module, put the warning in a BEGIN block in the module.

You can also put the warnings in the import method (e.g. Win32::GUI::import): It all depends on exactly what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文