为什么 Perl::Critic 不喜欢使用 shift 来填充子例程变量?
最近,我决定开始在我的网站上更频繁地使用 Perl::Critic代码。在使用 Perl 编程近 7 年之后,我已经习惯了大多数 Perl 最佳实践很长一段时间,但我知道总有改进的空间。但一直困扰我的一件事是 Perl::Critic 不不喜欢我为子例程解压 @_ 的方式。举个例子:
sub my_way_to_unpack {
my $variable1 = shift @_;
my $variable2 = shift @_;
my $result = $variable1 + $variable2;
return $result;
}
这就是我一直这样做的方式,并且正如 PerlMonks 和 Stack Overflow 上讨论的那样,它的 也不一定是邪恶的。
将上面的代码片段更改为...
sub perl_critics_way_to_unpack {
my ($variable1, $variable2) = @_;
my $result = $variable1 + $variable2;
return $result;
}
...也有效,但我发现它更难阅读。我还读过 Damian Conway 的书 Perl 最佳实践,但我不太明白我的首选方法解包属于他的建议,避免直接使用 @_
,如 Perl::Critic 暗示着。我一直觉得康威在谈论肮脏的东西,例如:
sub not_unpacking {
my $result = $_[0] + $_[1];
return $result;
}
上面的例子很糟糕并且难以阅读,我永远不会考虑在一段生产代码中编写它。
简而言之,为什么 Perl::Critic 认为我的首选方式不好?难道我真的犯下了利用shift拆包的滔天大罪吗?
除了我之外,其他人是否认为应该通过 Perl::Critic< /a> 维护者?
Lately, I've decided to start using Perl::Critic more often on my code. After programming in Perl for close to 7 years now, I've been settled in with most of the Perl best practices for a long while, but I know that there is always room for improvement. One thing that has been bugging me though is the fact that Perl::Critic doesn't like the way I unpack @_ for subroutines. As an example:
sub my_way_to_unpack {
my $variable1 = shift @_;
my $variable2 = shift @_;
my $result = $variable1 + $variable2;
return $result;
}
This is how I've always done it, and, as its been discussed on both PerlMonks and Stack Overflow, its not necessarily evil either.
Changing the code snippet above to...
sub perl_critics_way_to_unpack {
my ($variable1, $variable2) = @_;
my $result = $variable1 + $variable2;
return $result;
}
...works too, but I find it harder to read. I've also read Damian Conway's book Perl Best Practices and I don't really understand how my preferred approach to unpacking falls under his suggestion to avoid using @_
directly, as Perl::Critic implies. I've always been under the impression that Conway was talking about nastiness such as:
sub not_unpacking {
my $result = $_[0] + $_[1];
return $result;
}
The above example is bad and hard to read, and I would never ever consider writing that in a piece of production code.
So in short, why does Perl::Critic consider my preferred way bad? Am I really committing a heinous crime unpacking by using shift?
Would this be something that people other than myself think should be brought up with the Perl::Critic maintainers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的答案是 Perl::Critic 在这里没有遵循 PBP。这
书中明确指出,转变习惯用法不仅可以接受,而且
在某些情况下实际上是首选。
The simple answer is that Perl::Critic is not following PBP here. The
book explicitly states that the shift idiom is not only acceptable, but
is actually preferred in some cases.
使用
--verbose 11
运行perlcritic
解释了这些策略。不过,这两种解释似乎都不适用于您。Running
perlcritic
with--verbose 11
explains the policies. It doesn't look like either of these explanations applies to you, though.重要的是要记住,Perl 最佳实践中的很多内容只是一个人对外观的看法最好的或最容易使用的,如果你用其他方式来做也没关系。达米安在本书的介绍性文字中也说了同样的话。这并不是说全部都是这样——其中有很多东西是绝对必要的:例如,使用
strict
。因此,当您编写代码时,您需要自己决定自己的最佳实践是什么,而使用 PBP 是一个很好的起点。然后与你自己的标准保持一致。
我尝试遵循 PBP 中的大部分内容,但 Damian 可以拥有我的子例程参数
shift
和我的es,除非他从我冰冷、死气沉沉的指尖撬开它们。
对于 Critic,您可以选择要执行的策略,甚至可以创建自己的策略(如果尚不存在)。
It's important to remember that a lot of the stuff in Perl Best Practices is just one guy's opinion on what looks the best or is the easiest to work with, and it doesn't matter if you do it another way. Damian says as much in the introductory text to the book. That's not to say it's all like that -- there are many things in there that are absolutely essential: using
strict
, for instance.So as you write your code, you need to decide for yourself what your own best practices will be, and using PBP is as good a starting point as any. Then stay consistent with your own standards.
I try to follow most of the stuff in PBP, but Damian can have my subroutine-argument
shift
s and myunless
es when he pries them from my cold, dead fingertips.As for Critic, you can choose which policies you want to enforce, and even create your own if they don't exist yet.
在某些情况下,Perl::Critic 无法精确地执行 PBP 指南,因此它可能会执行尝试符合 Conway 指南精神的近似值。我们完全有可能误解或误用了 PBP。如果您发现某些内容有问题,请将错误报告邮寄至 [email protected ] 我们会立即调查。
谢谢,
-杰夫
In some cases Perl::Critic cannot enforce PBP guidelines precisely, so it may enforce an approximation that attempts to match the spirit of Conway's guidelines. And it is entirely possible that we have misinterpreted or misapplied PBP. If you find something that doesn't smell right, please mail a bug report to [email protected] and we'll look into it right away.
Thanks,
-Jeff
我认为如果不是真的有必要,你通常应该避免轮班!
刚刚遇到这样的代码:
如果您开始更改此代码中的某些内容,那么您很可能会意外更改轮班顺序,或者可能跳过一个轮班,然后一切都会向南发展。此外,它很难阅读 - 因为你不能确定你真的看到了子程序的所有参数,因为下面的某些行可能是某个地方的另一个转变......如果你在中间使用一些正则表达式,它们可能会替换 $_ 和奇怪的事情开始发生...
使用解包 my (...) = @_ 的直接好处是您可以复制 (...) 部分并将其粘贴到调用方法的位置并有一个漂亮的签名: )您甚至可以事先使用相同的变量名称,而不必更改任何内容!
我认为移位意味着列表操作,其中列表的长度是动态的,并且您希望一次处理一个元素,或者您明确需要一个没有第一个元素的列表。但如果您只想将整个列表分配给 x 参数,您的代码应该使用 my (...) = @_; 来表示。没有人会感到奇怪。
I think you should generally avoid shift, if it is not really necessary!
Just ran into a code like this:
If you start changing something in this code, there is a good chance you might accidantially change the order of the shifts or maybe skip one and everything goes southway. Furthermore it's hard to read - because you cannot be sure you really see all parameters for the sub, because some lines below might be another shift somewhere... And if you use some Regexes in between, they might replace the contents of $_ and weird stuff begins to happen...
A direct benefit of using the unpacking my (...) = @_ is you can just copy the (...) part and paste it where you call the method and have a nice signature :) you can even use the same variable-names beforehand and don't have to change a thing!
I think shift implies list operations where the length of the list is dynamic and you want to handle its elements one at a time or where you explicitly need a list without the first element. But if you just want to assign the whole list to x parameters, your code should say so with my (...) = @_; no one has to wonder.