在检查字符串中子字符串的索引时违反 Perl 评论家策略 [英] Perl critic policy violation in checking index of substring in a string

查看:33
本文介绍了在检查字符串中子字符串的索引时违反 Perl 评论家策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for my $item (@array) {
    if (index($item, '$n') != -1) {
       print "HELLO\n";
    }
} 

问题是:Perl 评论家给出了以下违规行为.

Problem is: Perl critic gives below policy violation.

字符串可能需要在第 168 行,靠近 '$item, '$n'' 处进行插值.(严重性:1)

String may require interpolation at line 168, near '$item, '$n''. (Severity: 1)

请告诉我如何解决这个问题?

Please advise how do I fix this?

推荐答案

在这种情况下,分析器要么发现了错误,要么在标记代码时明显错误.

In this case the analyzer either found a bug or is plain wrong in flagging your code.

您是要在 $item 中查找文字$n",还是要查找 $n 变量的计算结果?

Are you looking for a literal "$n" in $item, or for what $n variable evaluates to?

  • 如果你想找到文字 $n 字符,那么你的代码没有问题

  • If you want to find the literal $n characters then there is nothing wrong with your code

如果您希望 $item 包含存储在 $n 变量中的值,则允许对其进行评估,

If you expect $item to contain the value stored in $n variable then allow it to be evaluated,

if (index($item, $n)  != -1)

如果确实如此,但 $n 还可能包含其他转义序列或编码,您需要将其作为文字字符(以便抑制它们的评估),那么您可能需要做更多的工作,具体取决于该变量中可能包含的内容.

If this is indeed the case but $n may also contain yet other escaped sequences or encodings which you need as literal characters (so to suppress their evaluation) then you may need to do a bit more, depending of what exactly may be in that variable.

如果您确实需要查找字符 $ 后跟 n(这可以解释故意在变量周围放置单引号的行为),您需要处理警告.

In case you do need to find characters $ followed by n (what would explain a deliberate act of putting single quotes around a variable) you need to handle the warning.

对于违反的特定政策,请参阅 Perl::Critic::Policy::ValuesAndExpressions

For the particular policy that is violated see Perl::Critic::Policy::ValuesAndExpressions

如果您将单引号或 q//与包含可能需要插值的未转义元字符的字符串一起使用,则此政策会向您发出警告.

This policy warns you if you use single-quotes or q// with a string that has unescaped metacharacters that may need interpolation.

要满足策略,您需要使用双引号并转义 $,例如 qq(\$n).在我看来,这会将原本精美的代码段变成看起来很奇怪的东西.

To satisfy the policy you'd need to use double quotes and escape the $, for example qq(\$n). In my opinion this would change the fine original code segment into something strange to look at.

如果您最终只想让警告静音,请参阅文档,违反规则

If you end up wanting to simply silence the warning see documentation, in Bending The Rules

评论.工具 perlcritic 很有用,但您必须正确使用它.它是一个静态代码分析器,它不知道你的程序在做什么,可以这么说;它可以捕捉不良做法,但不能告诉您如何编写程序.它的许多策略"不适用于特定代码.

A comment. The tool perlcritic is useful but you have to use it right. It's a static code analyzer and it doesn't know what your program is doing, so to say; it can catch bad practices but can't tell you how to write programs. Many of its "policies" are unsuitable for particular code.

它基于的书在介绍中很好地说明了这一切.合理使用.

The book that it is based on says all this very nicely in its introduction. Use sensibly.

当我查看 问题 时,似乎您正在寻找匹配子字符串的索引,因此您需要 $n 变量的内容,而不是文字$n".然后 perlcritic 发现了代码中的一个错误,使用它有很好的回报!

When I look at the question where this comes from it appears that you are looking for index at which substrings were matched, so you need the content of $n variable, not literal "$n". Then perlcritic identified a bug in the code, good return for using it!

这篇关于在检查字符串中子字符串的索引时违反 Perl 评论家策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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