为什么不使用“opendir”?与“开放”具有相同的政策; Perl 最佳实践?
为什么 opendir
没有与 Perl 最佳实践?
我至少在考虑这两个策略:
Is there a reason why opendir
doesn't have the same policy than open
in Perl Best Practices?
I'm thinking about at least these 2 policies:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Perl Best Practices 的原始规则(对于您提到的第一个策略)是
,它不仅仅适用于
打开
。 Perl::Critic 很大程度上基于 PBP,但它确实有所不同(与 perldoc):因此,Perl::Critic 没有在 opendir 上强制执行相同的规则这一事实可能主要是一个疏忽。这也可能归咎于 PBP 中的示例仅使用
open
。我建议在 CPAN 上提交一个错误(查看代码,这只是一行更改)。第二条规则实际上并非来自 PBP,但在我看来它同样适用于 opendir。同样,向 CPAN 上的作者提交错误报告将是一个好主意,因为它也只是一行更改。如果这实际上是一个有意的决定,您可能会得到更具体的反馈。
更正:有点不同,但 PBP 中第二个策略最接近的规则是
修复该策略将不仅仅是一个行,但如果维护者认为有必要(并且不担心它会破坏太多现有代码),那么仍然相对容易。
The original rule from Perl Best Practices (for the first Policy you mention) was
which applies to much more than just
open
. Perl::Critic is based in large part on PBP but it does differ (from the perldoc):So the fact that Perl::Critic doesn't enforce the same rule on opendir is probably mostly an oversight. It could also be blamed on the fact that the examples in PBP only use
open
. I would suggest submitting a bug on CPAN (looking at the code, it would only be a one line change).The second rule doesn't actually come from PBP but it seems to me it is just as applicable to opendir. Again, a bug report to the author on CPAN would be a good idea since it would again only be a one line change. And you might get more specific feedback if in fact it was an intentional decision.
Correction: it's a little different but the closest rule in PBP for the second Policy is
and fixing that policy would be more than a one liner but still relatively easy if the maintainer thought it warranted (and wasn't worried that it would break too much existing code).
大胆猜测:文件的使用更加频繁,而且还没有人抽出时间在目录句柄上进行“lint-ish”工作。
仅供参考:如果你真的想强制执行这些,你可以实现一些东西来模拟 Ruby open + block 比喻:创建一个 open/close 包装器 sub,它接受另一个 sub 作为参数来执行打开和关闭之间的实际 I/O。我可以添加一个代码示例,但我有点偏离主题。
Wild guess: files are used much more often, and nobody got around to doing the "lint-ish" work on directory handles yet.
FYI: if you really wanted to enforce these, you could implement something to emulate the Ruby open + block metaphor: Make an open/close wrapper sub which takes another sub as a parameter to do the actual I/O between the open and close. I could put a code sample in, but I'm kind of wandering off topic.
请问作者?我知道这似乎是一种聪明的答案,但查看源代码是了解原因的最简单方法
ask the author? I know it seems kind of smart-arsed answer, but going to the source is the easiest way to learn why