为什么使用警告;使用严格;在 Perl 中不是默认的? [英] Why are use warnings; use strict; not default in Perl?

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

问题描述

我想知道为什么

use warnings;
use strict;

在 Perl 中不是默认的.每个脚本都需要它们.如果有人(出于充分的理由)需要禁用它们,他们应该使用 no strict 和/或应该使用一些命令行参数(对于单行程序).

are not default in Perl. They're needed for every script. If someone (for good reason) needs to disable them, they should use no strict and/or should use some command line argument (for one-liners).

是否有太多写得不好的 CPAN 模块(使用badly"表示没有 use strict)?还是因为这会破坏大量已经在生产中的代码?我确定这是有原因的,我很想知道.

Are there too many badly-written CPAN modules (using "badly" to mean without use strict)? Or is it because this can break a lot of code already in production? I'm sure there is a reason and I would like to know it.

在 5.14 中 IO::File 是根据需要自动加载的,用这些基本的编译指示做类似的事情难道不可能吗?

In 5.14 IO::File is loaded automagically on demand, wouldn't it be possible to do something like that with these basic pragmas?

推荐答案

这是为了向后兼容.Perl 4 根本没有严格,而且很可能仍然存在最初为 Perl 4 编写的脚本,仍然可以在 Perl 5 中正常工作.使严格自动化会破坏这些脚本.对于 one-liners 来说情况更糟,其中许多不需要声明变量.默认情况下使单行程序严格可能会破坏数百万个 shell 脚本和 Makefile.

It's for backwards compatibility. Perl 4 didn't have strict at all, and there are most likely still scripts out there originally written for Perl 4 that still work fine with Perl 5. Making strict automatic would break those scripts. The situation is even worse for one-liners, many of which don't bother to declare variables. Making one-liners strict by default would break probably millions of shell scripts and Makefiles out there.

它不能自动加载,因为它增加了限制,而不是功能.在文件句柄上调用方法时加载 IO::File 是一回事.但是除非代码做了严格禁止的事情,否则激活严格是没有意义的.

It can't be loaded automagically, because it adds restrictions, not features. It's one thing to load IO::File when a method is called on a filehandle. But activating strict unless the code did something prohibited by strict is meaningless.

如果脚本指定最低版本为 5.11.0 或更高(例如 use 5.012),则 strict 自动开启.这不会启用警告,但可能会在未来版本中添加.此外,如果您在 Perl 中进行 OO 编程,您应该知道使用 Moose 会自动打开两个 <该类中的 code>strict 和 warnings.

If a script specifies a minimum version of 5.11.0 or higher (e.g. use 5.012), then strict is turned on automatically. This doesn't enable warnings, but perhaps that will be added in a future version. Also, if you do OO programming in Perl, you should know that using Moose automatically turns on both strict and warnings in that class.

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

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