1Perl::Critic::Policy::IUPnseperurlt:OC:uoCtnrptiurttii:bc:u:Rt:eePqdouliPireceryBl:r:iDIeonfcpOuupmteeOnnu(tt3apptumit)o:n:RequireBriefOpen(3pm)
2
3
4

NAME

6       Perl::Critic::Policy::InputOutput::RequireBriefOpen - Close filehandles
7       as soon as possible after opening them.
8

AFFILIATION

10       This Policy is part of the core Perl::Critic distribution.
11

DESCRIPTION

13       One way that production systems fail unexpectedly is by running out of
14       filehandles.  Filehandles are a finite resource on every operating
15       system that I'm aware of, and running out of them is virtually
16       impossible to recover from.  The solution is to not run out in the
17       first place.  What causes programs to run out of filehandles?  Usually,
18       it's leaks: you open a filehandle and forget to close it, or just wait
19       a really long time before closing it.
20
21       This problem is rarely exposed by test systems, because the tests
22       rarely run long enough or have enough load to hit the filehandle limit.
23       So, the best way to avoid the problem is 1) always close all
24       filehandles that you open and 2) close them as soon as is practical.
25
26       This policy takes note of calls to open() where there is no matching
27       close() call within "N" lines of code.  If you really need to do a lot
28       of processing on an open filehandle, then you can move that processing
29       to another method like this:
30
31           sub process_data_file {
32               my ($self, $filename) = @_;
33               open my $fh, '<', $filename
34                   or croak 'Failed to read datafile ' .  $filename . '; ' . $OS_ERROR;
35               $self->_parse_input_data($fh);
36               close $fh;
37               return;
38           }
39           sub _parse_input_data {
40               my ($self, $fh) = @_;
41               while (my $line = <$fh>) {
42                   ...
43               }
44               return;
45           }
46
47       As a special case, this policy also allows code to return the
48       filehandle after the "open" instead of closing it.  Just like the
49       close, however, that "return" has to be within the right number of
50       lines.  From there, you're on your own to figure out whether the code
51       is promptly closing the filehandle.
52
53       The STDIN, STDOUT, and STDERR handles are exempt from this policy.
54

CONFIGURATION

56       This policy allows close() invocations to be up to "N" lines after
57       their corresponding open() calls, where "N" defaults to 9.  You can
58       override this to set it to a different number with the "lines" setting.
59       To do this, put entries in a .perlcriticrc file like this:
60
61         [InputOutput::RequireBriefOpen]
62         lines = 5
63

CAVEATS

65   "IO::File->new"
66       This policy only looks for explicit "open" calls.  It does not detect
67       calls to "CORE::open" or "IO::File->new" or the like.
68
69   Is it the right lexical?
70       We don't currently check for redeclared filehandles.  So the following
71       code is false negative, for example, because the outer scoped
72       filehandle is not closed:
73
74           open my $fh, '<', $file1 or croak;
75           if (open my $fh, '<', $file2) {
76               print <$fh>;
77               close $fh;
78           }
79
80       This is a contrived example, but it isn't uncommon for people to use
81       $fh for the name of the filehandle every time.  Perhaps it's time to
82       think of better variable names...
83

CREDITS

85       Initial development of this policy was supported by a grant from the
86       Perl Foundation.
87

AUTHOR

89       Chris Dolan <cdolan@cpan.org>
90
92       Copyright (c) 2007-2023 Chris Dolan
93
94       This program is free software; you can redistribute it and/or modify it
95       under the same terms as Perl itself.  The full text of this license can
96       be found in the LICENSE file included with this module
97
98
99
100perl v5.38.0          Perl::Critic2:0:2P3o-l0i9c-y2:5:InputOutput::RequireBriefOpen(3pm)
Impressum