开发者

Why doesn't 'opendir' have the same policy as 'open' in Perl Best Practices?

开发者 https://www.devze.com 2022-12-15 07:18 出处:网络
Is there a reaso开发者_Python百科n why opendir doesn\'t have the same policy than open in Perl Best Practices?

Is there a reaso开发者_Python百科n why opendir doesn't have the same policy than open in Perl Best Practices?

I'm thinking about at least these 2 policies:

  • Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles
  • Perl::Critic::Policy::InputOutput::RequireBriefOpen


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消