Search code examples
perlperl-critic

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


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:


Solution

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