为什么使用严格和警告? [英] Why use strict and warnings?

查看:43
本文介绍了为什么使用严格和警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,如果人们使用:

It seems to me that many of the questions in the Perl tag could be solved if people would use:

use strict;
use warnings;

我认为有些人认为这些类似于训练轮或不必要的并发症,这显然不是真的,因为即使是非常熟练的 Perl 程序员也会使用它们.

I think some people consider these to be akin to training wheels, or unnecessary complications, which is clearly not true, since even very skilled Perl programmers use them.

似乎大多数精通 Perl 的人总是使用这两个 pragma,而那些从使用它们中获益最多的人很少这样做.因此,我认为在鼓励人们使用严格warnings时链接到一个问题是个好主意.

It seems as though most people who are proficient in Perl always use these two pragmas, whereas those who would benefit most from using them seldom do. So, I thought it would be a good idea to have a question to link to when encouraging people to use strict and warnings.

那么,为什么 Perl 开发人员应该使用strictwarnings?

So, why should a Perl developer use strict and warnings?

推荐答案

对于初学者,use strict;(在较小程度上,use warnings;)有助于找到变量名中的拼写错误.即使是有经验的程序员也会犯这样的错误.一个常见的情况是在清理或重构代码时忘记重命名变量的实例.

For starters, use strict; (and to a lesser extent, use warnings;) helps find typos in variable names. Even experienced programmers make such errors. A common case is forgetting to rename an instance of a variable when cleaning up or refactoring code.

使用 use strict;使用警告; 比其他方式更早地捕获许多错误,从而更容易找到错误的根本原因.根本原因可能是需要进行错误或验证检查,无论程序员技能如何,都可能发生这种情况.

Using use strict; use warnings; catches many errors sooner than they would be caught otherwise, which makes it easier to find the root causes of the errors. The root cause might be the need for an error or validation check, and that can happen regardless or programmer skill.

Perl 警告的优点在于它们很少是虚假的,因此使用它们几乎没有成本.

What's good about Perl warnings is that they are rarely spurious, so there's next to no cost to using them.

相关阅读:为什么使用my?

这篇关于为什么使用严格和警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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