如何将 Perl 代码标记为已弃用?
在我的项目中,我目前正在准备从遗留代码逐步迁移到经过正确设计和测试的新模块。由于并非每个程序员同事都密切关注我所做的事情,因此我想在使用旧代码时发出警告。我也非常希望能够输出有关如何移植旧代码的建议。
我找到了两种方法:
Attribute::Deprecated ,这对于函数来说很好,但如果不推荐使用完整的模块,则相当麻烦。此外,除了警告之外,没有其他信息。
- 用于模块的
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:
Attribute::Deprecated, which is fine for functions, but rather cumbersome if a complete module is deprecated. Also, no additional information apart from warnings.
Perl::Critic::Policy::Modules::ProhibitEvilModules
for modules or maybe a customPerl::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于方法和函数,您只需用警告和对首选函数的调用替换函数体即可。
perl perllexwarn 给出以下示例:
如果您要弃用整个模块,将警告放在模块的
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:
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.