Can 'use strict' warn instead of error

前端 未结 5 1166
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 04:31

When using use strict perl will generate a runtime error on unsafe constructs. Now I am wondering if it is possible to have it only print a warning instead of c

5条回答
  •  无人共我
    2021-01-19 04:51

    I'm gonna take a stab at guessing the real motivation here. Feel free to tell me if I guessed wrong.

    I suspect your trying to tackle a large, older code base and would like to enable strictures but you were hoping first to get a sense of where the errors will be (and how many there are) without breaking functionality. Unfortunately, since use strict functions by modifying the internal behavior of the perl parser and interpreter, there isn't a 'loose strict' or, by analogy to html, any kind of 'transitional' mode.

    However, you can tease apart the functionality of use strict to start moving in the right direction. First, note that there are actually three separate parts:

    use strict 'refs'; # no symbolic references
    use strict 'vars'; # must declare variables
    use strict 'subs'; # no barewords
    

    and of those only 'refs' generates runtime errors. So you could easily add use strict qw(vars subs) to each of your files (scripts and modules) and test them with perl -c. If you encounter any error messages, then comment out the use strict, or at least whichever of the two checks failed, and add a comment as to the nature of the failure and move on. This way you can quickly (depending on the number of files) determine which files have compile-time errors and come back to address them later. (If you were more motivated than me at the moment, you could even automate this process). Unless you have code that does scary things inside of BEGIN blocks, this should be pretty safe to do.

    The trickier part is checking for the runtime errors generated by use strict 'refs' and unfortunately, there really isn't an easy way to do this because the errors are triggered by symbolic references which can't be determined by any kind of static analysis so -c and/or Perl::Critic are both useless.

    Hopefully that gets closer to addressing your real problem.

提交回复
热议问题