为什么不在 Perl 中使用严格和警告? [英] Why not use strict and warnings in Perl?

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

问题描述

我已经看到混淆和打高尔夫球的代码保持关闭以避免声明变量,我可以看到在命令行上使用 -e 开关跳过它们以保持单行更短.在哪些情况下您不希望在生产代码中use strict 和/或use warnings?您不希望的原因是什么?使用它们?

I've seen obfuscated and golfed code that keeps is off to avoid declaring variables, and I can see skipping them on the command line with the -e switch to keep the one-liner shorter. What are some cases where you would not want to use strict and/or use warnings in production code? What are reasons you would not want to use them?

问题出现是因为我在这里看到有经验的 Perl 用户告诉 Perl 新手总是使用它们的帖子.

The question comes up because I've seen posts on here where experienced Perl users tell people who are new to Perl to always use them.

我确实在这里找到了一些相关问题,但它们没有解释我们可能希望将它们关闭的情况.

I did find some related questions on here but they don't explain cases where we might want to keep them turned off.

推荐答案

strict 编译指示将您限制为 Perl 的一个健全的子集.一些旧功能具有历史意义(或具有单行代码的好处),但在现代、可读的代码库中没有立足之地.

The strict pragma restricts you to a sane subset of Perl. Some old features make historic sense (or have benefits with one-liners), but have no place in a modern, readable code base.

strict 编译指示分为三类:

  • "vars" 强制您声明所有变量.这可以防止拼写错误,并确保您选择一个范围(全局/词法).在单行中,这不需要那么多,因为通常只有很少的范围和很少的变量.一些单行习语不能仅用于词法变量.

  • "vars" Forces you to declare all your variables. This shields against typos, and makes sure you pick a scope (global/lexical). In a one-liner, this is not needed that much, as there are usually very few scopes, and very few variables. Some one-liner idioms wouldn't work with lexical variables only.

"refs" 不允许使用 symrefs.它们对词法变量没有意义,而 Perl5 有真正的引用.所以它们通常是无用的.但是,symrefs 对于元编程仍然很有价值:

"refs" disallows symrefs. They make no sense with lexical variables, and Perl5 has real references. So they are generally useless. However, symrefs remain valuable for metaprogramming:

# generate accessors
for my $field (qw/foo bar baz/) {
  no strict 'refs';
  *{ __PACKAGE__ . '::' . $field } = sub { shift()->{$field} };
}

  • "subs" 强制将大多数裸字解释为子程序调用.这解决了 foo 的歧义."bar"foo() .条".如果此类别未激活,并且当前未定义 foo sub,则它将被解析为 "foo" .条".这对 shell 程序员来说是有意义的,其中所有的裸词都是字符串.但是在 Perl 程序中,这大大增加了程序员的认知负担,不值得.

  • "subs" forces the interpretation of most barewords as subroutine calls. This resolves the ambiguity of foo . "bar" to be foo() . "bar". If this category is not activated, and if no foo sub is currently defined, then it would have parsed as "foo" . "bar". This makes sense to a shell programmer, where all barewords are strings. But in a Perl program, this drastically increases the cognitive load of the programmer, and is not worth it.

    总结:对于没有优化可读性的简单脚本,strict "vars" 并不是真正必要的.在某些情况下,不需要严格的 'refs'.

    Summary: for simple scripts that don't optimize for readability, strict "vars" isn't really neccessary. There are a few cases where no strict 'refs' is desired.

    warnings 编译指示允许对警告消息进行细粒度控制.这对于 Perl 新手尤其重要,他们经常编写诸如

    The warnings pragma allows fine grained control over warning messages. This is especially important for programmers new to Perl, who frequently write stuff like

    my %hash = { foo => 1, bar => 2 };
    

    并想知道 HASH(0x1234567) 键从何而来.即使在单行模式上,警告也是可取的,除非您使用 undef 等的字符串化

    and wonder where that HASH(0x1234567) key came from. Even on a one-liner, warnings are desirable, except in cases where you use stringification of undef etc.

    在专业的代码库中,没有理由不使用warnings 无处不在.如果脚本发出警告,则很可能存在错误,并且 no warnings 不会使此错误消失.您对 Perl 的了解永远不会像 warnings pragma 那样广泛.即使是大师也会犯错误.use warnings 是一个很好的调试捷径.

    In a professional codebase, there is no excuse for not using warnings everywhere. If a script warns, it's very likely there is a bug, and no warnings does not make this bug go away. Your knowledge of Perl is never as vast as that of the warnings pragma. Even gurus make mistakes. use warnings is a great debugging shortcut.

    也就是说,在部署程序时注释 use warnings 可能没问题.但绝不是为了开发.

    That said, it may be allright to comment the use warnings when deploying the program. But never for development.

    根据开发团队的共识,还应使用其他编译指示:

    Depending on the consensus in the dev team, other pragmas should be used as well:

    • noindirect 不允许讨厌的 new Foo 方法调用.我见过一些可能在编译时使用此 pragma 捕获的错误潜入.
    • no autovivification 防止引用在诸如 $hash{doesnt_exist}{foo} 之类的只读操作中出现.
    • no indirect disallows the loathed new Foo method calls. I've seen bugs sneak in that could have been caught at compile time with this pragma.
    • no autovivification prevents references springing into existence on read-only operations like $hash{doesnt_exist}{foo}.

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

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