你在 Perl 中使用哪个安全网? [英] Which safety net do you use in Perl?

查看:27
本文介绍了你在 Perl 中使用哪个安全网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您使用哪个安全网?

使用警告;

使用严格;

我知道

使用中发现的潜在问题严格的;会导致你的代码停止一遇到就立刻使用警告时;只会给出一个警告(如命令行开关-w) 并让您的代码运行.

A potential problem caught by use strict; will cause your code to stop immediately when it is encountered, while use warnings; will merely give a warning (like the command-line switch -w) and let your code run.

我仍然想知道 Perl 程序员最常使用哪一个.他们见过使用最多的是哪一种?

Still I want to know that which one is mostly used by the Perl-programmers. Which one they have seen being used the most?

推荐答案

use strict 如果使用符号引用(即表示符号名称的字符串)会产生错误.如果你使用一个变量而不声明它,它会产生一个错误(这鼓励使用词法 'my' 变量,但如果你正确声明包全局变量也能满足).如果您在脚本中留下裸字(未加引号的字符串,本质上是 Perl 对引号的定义),它也会产生错误.使用strict",您可以启用或禁用三类限制中的任何一种,我在作用域块内这样做.启用限制是最佳实践,但有时合法代码需要在本地禁用其某些功能.然而,人们应该仔细思考这是否真的有必要,以及他们的解决方案是否理想.您可以在 Perl 的名为strict"的 POD 中阅读有关限制的信息.

use strict generates an error if you use symbolic references (ie, strings to represent names of symbols). It generates an error if you use a variable without declaring it (this encourages the use of lexical 'my' variables, but is also satisfied if you properly declare package globals). It also generates an error if you leave barewords hanging around in the script (unquoted strings, essentially, by Perl's definition of quotes). With 'strict', you may enable or disable any of three categories of strictures, and my do so within scoped blocks. It is a best practice to enable strictures, though on occasion legitimate code requires that some of its features be locally disabled. However, one should think long and hard about whether this is really necessary, and whether their solution is ideal. You can read about strictures in Perl's POD entitled 'strict'.

use warnings 根据许多标准生成警告消息,这些标准在 POD 'perllexwarn' 中进行了描述.这些警告与限制无关,而是要注意在他们的编程中可能遇到的最常见的陷阱".在编写脚本时使用警告也是最佳实践.在消息可能不受欢迎的某些情况下,可能会在范围内本地禁用某个警告类别.附加信息在警告"中描述.

use warnings generates a warning message based on many criteria, which are described in the POD 'perllexwarn'. These warnings have nothing to do with strictures, but rather, watch for the most common "gotchas" one is likely to encounter in their programming. It is a best practice to use warnings while writing scripts too. In some cases where the message might be undesirable a certain warning category may be locally disabled within a scope. Additional info is described in 'warnings'.

use diagnostics 使警告更加冗长,在开发或学习环境中,尤其是在新手中,这是非常可取的.诊断可能会被排除在最终产品"之外,但在开发过程中,它们可以成为通常生成的简洁消息的一个非常好的补充.您可以在 Perl POD诊断"中阅读有关诊断的信息.

use diagnostics makes the warnings more verbose, and in a development or learning environment, particularly among newcomers, that's highly desirable. Diagnostics would probably be left out of a 'final product', but while in development they can be a really nice addition to the terse messages normally generated. You can read about diagnostics in the Perl POD "diagnostics."

没有理由强迫自己只使用上述选项中的一种或另一种.特别是,在现代 Perl 程序中通常应该同时使用 use warnings 和 use strict.

There is no reason to force oneself to use only one of the above options or another. In particular, use warnings and use strict should generally both be used in modern Perl programs.

在所有情况下(除了诊断,您无论如何只能将其用于开发),个别限制或警告可能在词法上被禁用.此外,它们的错误可能会被 eval{ .... }Try::Tiny 的 try/catch 块和其他一些方法捕获.如果担心消息会向潜在攻击者提供有关脚本的更多信息,则可以将消息路由到日志文件.如果存在上述日志文件占用大量空间的风险,则说明手头有更大的问题,应该解决问题的根源,或者在极少数情况下只需禁用该消息.

In all cases (except diagnostics, which you're only using for development anyway), individual strictures or warnings may be lexically disabled. Furthermore, their errors may be trapped with eval{ .... }, with Try::Tiny's try/catch blocks, and a few other ways. If there's a concern about a message giving a potential attacker more information about a script, the messages could be routed to a logfile. If there's a risk of said logfile consuming lots of space, there's a bigger issue at hand, and the source of the issue should either be resolved or in some rare cases simply have the message disabled.

现在的 Perl 程序应该高度严格/遵守警告作为最佳实践.

Perl programs nowadays should be highly strict/warnings compliant as a best practice.

这篇关于你在 Perl 中使用哪个安全网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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