5

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:

toolic
  • 57,801
  • 17
  • 75
  • 117
sebthebert
  • 12,196
  • 2
  • 26
  • 37
  • 2
    Cool, thanks for the tip on the perl (5.6+) tools. I had never seen the Critic package before, nor the non-bareword file handles. – Roboprog Dec 31 '09 at 00:28
  • @Roboprog: please see http://stackoverflow.com/questions/1479741/why-is-three-argument-open-calls-with-lexical-filehandles-a-perl-best-practice and http://stackoverflow.com/questions/tagged/perl-critic – Ether Dec 31 '09 at 00:34
  • 2
    @Roboprog: anyone still using 5.005 or earlier (and I know there are still quite a few) should at this point just be assuming new things they hear about won't work for them. 5.6 is just 11 weeks short of its 10th birthday! – ysth Dec 31 '09 at 00:38
  • Link to top level docs for Critic: http://search.cpan.org/~elliotjs/Perl-Critic-1.105/lib/Perl/Critic.pm (for those like me to whom it is new -- I don't do as much Perl as I used to) – Roboprog Dec 31 '09 at 00:38
  • 3
    That link will fail after a new version comes out. One that will always work is http://search.cpan.org/perldoc/Perl::Critic – Ether Dec 31 '09 at 00:40
  • Unfortunately, back in 1999 we still had many hosts with perl 4 at my workplace at the time. It's amazing how far (YEARS) behind a large company can have and keep things, and often for good-enough reasons :-( – Roboprog Dec 31 '09 at 00:40
  • Cool, thanks for the "3 arg open" reference as well. If I ever get back into doing a bunch of Perl again, I definitely need to scan the "best practices" list, and determine what the "least common denominator" is for the available installations. My Blue Camel is more than a little aged, it seems, as a standard.... And finally, does this mean I shouldn't use "local" any more, either? :-) – Roboprog Dec 31 '09 at 00:50
  • @Roboprog: since you're asking, probably not, although `local` does have its uses. Search around here and you'll find a good discussion, or open up a new question. – Ether Dec 31 '09 at 18:09

3 Answers3

3

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
  • 7,812
  • 3
  • 31
  • 34
0

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
  • 3,054
  • 2
  • 28
  • 27
0

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
  • 1