可以使用严格的警告而不是错误 [英] Can 'use strict' warn instead of error

查看:82
本文介绍了可以使用严格的警告而不是错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用使用strict perl将在不安全的结构上生成运行时错误。现在我想知道是否可以只打印一个警告而不是导致运行时错误?或者是使用警告(或-w)警告相同的问题?

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 causing a runtime error ? Or is use warnings (or -w) warning about the same issues ?

推荐答案

我想在这里猜测真正的动机。如果我猜错了,请随时告诉我。

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

我怀疑你试图解决一个较大的旧的代码基础,并希望启用限制,但是你希望得到感觉错误将在哪里(以及有多少),而不会破坏功能。不幸的是,由于通过修改Perl解析器和解释器的内部行为来使用严格的函数,所以没有一个宽松的严格,或者通过类比的HTML, 过渡模式。

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.

但是,您可以分开使用使用strict 在右侧开始移动的功能方向。首先,请注意,实际上有三个不同的部分:

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

只有ref会生成运行时错误。所以你可以轻松地向每个文件(脚本和模块)添加使用strict qw(vars subs),并用 perl -c 。如果您遇到任何错误消息,请注释出使用strict ,或至少两个检查中的哪一个失败,并添加关于故障性质的注释并移动上。这样,您可以快速(取决于文件数)确定哪些文件具有编译时错误,然后再回来解决它们。 (如果你现在比我更有动力,甚至可以自动化这个过程)。除非你有代码在 BEGIN 块之内可怕的东西,这应该是非常安全的。

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.

棘手的部分是检查由使用strict'refs'生成的运行时错误,不幸的是,真的不是一个简单的方法,因为错误是由符号引用触发的这不能通过任何类型的静态分析来确定,如-c和/或 Perl ::批评者都是无用的。

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.

这篇关于可以使用严格的警告而不是错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆