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

查看:73
本文介绍了为什么使用警告?使用严格在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模块(使用严重"来表示没有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上正常工作.使严格的自动操作会破坏这些脚本.对于单行代码而言,情况甚至更糟,其中很多都不必费心声明变量.默认情况下,严格限制单行代码可能会破坏数百万个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 会自动打开两个<该类中的c4>和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天全站免登陆