Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mark Perl code as deprecated?

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?

like image 986
Nikolai Prokoschenko Avatar asked Nov 09 '09 13:11

Nikolai Prokoschenko


1 Answers

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.

like image 192
Sinan Ünür Avatar answered Nov 13 '22 21:11

Sinan Ünür