为什么不使用“opendir”?与“开放”具有相同的政策; Perl 最佳实践?

发布于 2024-08-16 14:46:36 字数 571 浏览 11 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-08-23 14:46:36

Perl Best Practices 的原始规则(对于您提到的第一个策略)是

不要使用裸字文件句柄

,它不仅仅适用于打开。 Perl::Critic 很大程度上基于 PBP,但它确实有所不同(与 perldoc):

Perl::Critic 是一个可扩展框架,用于创建编码标准并将其应用于 Perl 源代码。本质上,它是一个静态源代码分析引擎。 Perl::Critic 与许多 Perl::Critic::Policy 模块一起分发,这些模块试图强制执行各种编码准则。大多数策略模块都基于 Damian Conway 的《Perl 最佳实践》一书。 但是,Perl::Critic 不仅限于 PBP,甚至会支持与 Conway 相矛盾的策略。

因此,Perl::Critic 没有在 opendir 上强制执行相同的规则这一事实可能主要是一个疏忽。这也可能归咎于 PBP 中的示例仅使用 open。我建议在 CPAN 上提交一个错误(查看代码,这只是一行更改)。

第二条规则实际上并非来自 PBP,但在我看来它同样适用于 opendir。同样,向 CPAN 上的作者提交错误报告将是一个好主意,因为它也只是一行更改。如果这实际上是一个有意的决定,您可能会得到更具体的反馈。

更正:有点不同,但 PBP 中第二个策略最接近的规则是

尽快显式关闭文件句柄。

修复该策略将不仅仅是一个行,但如果维护者认为有必要(并且不担心它会破坏太多现有代码),那么仍然相对容易。

The original rule from Perl Best Practices (for the first Policy you mention) was

Don't use bareword filehandles

which applies to much more than just open. Perl::Critic is based in large part on PBP but it does differ (from the perldoc):

Perl::Critic is an extensible framework for creating and applying coding standards to Perl source code. Essentially, it is a static source code analysis engine. Perl::Critic is distributed with a number of Perl::Critic::Policy modules that attempt to enforce various coding guidelines. Most Policy modules are based on Damian Conway's book Perl Best Practices. However, Perl::Critic is not limited to PBP and will even support Policies that contradict Conway.

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

Close filehandles explicitly, and as soon as possible.

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).

嘿嘿嘿 2024-08-23 14:46:36

大胆猜测:文件的使用更加频繁,而且还没有人抽出时间在目录句柄上进行“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.

心作怪 2024-08-23 14:46:36

请问作者?我知道这似乎是一种聪明的答案,但查看源代码是了解原因的最简单方法

ask the author? I know it seems kind of smart-arsed answer, but going to the source is the easiest way to learn why

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文