tags:

views:

155

answers:

3

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:

A: 

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.

Roboprog
A: 

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

tish
+2  A: 

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

Rob Van Dam