NAME
Perl::Critic::Policy::InputOutput::RequireBriefIOOpen - Close a handle from IO::File->new and the other IO::* openers soon after it opens.
VERSION
version 0.001
SYNOPSIS
my $path;
sub slurp {
my $fh = IO::File->new( $_[0], 'r' ) or die "$_[0]: $!";
local $/;
return <$fh>; # the scope ends: not reported
}
my $log = IO::File->new( $path, 'a' ) or die "$path: $!";
... # many lines, and no close: reported
DESCRIPTION
InputOutput::RequireBriefOpen reports a handle from open that is not closed within a few lines of the open. This policy does the same for a handle from an IO::* constructor or opener, such as IO::File->new(...) or $fh->open(...). Which calls those are, and how to configure them, is the business of Perl::Critic::Policy::InputOutput::RequireCheckedIOOpen, which this policy is a subclass of.
Which handles
The handle is a scalar that the call opens. A constructor must be assigned to it, as my $fh = IO::File->new(...) or $fh = IO::File->new(...). An opener must be called on it, as $fh->open(...). A handle that is stored straight into a hash or an array, or returned at once, belongs to the code that keeps it, and is left alone.
Let go in time
Within lines lines of the call, one of these must happen:
The scope of the handle ends. A lexical, declared with
my, is destroyed at the end of the block that declares it, and IO::Handle closes it then. Amyin the condition of anifor a loop lives to the end of that whole statement. The scope of a lexical of the file, of astatevariable, and of a package variable never ends in time.It is closed, with
$fh->close,close $fh,undef $fhor$fh = undef.Something takes it over: a
return, as inInputOutput::RequireBriefOpen, or a copy of it into another variable, an array, a hash or a closure.
A close or a return inside a sub that is defined there does not count, because it does not run there. Neither does anything in the statement of the call itself, such as the return of IO::File->new(...) or return, which runs only when the call fails.
CONFIGURATION
[InputOutput::RequireBriefIOOpen]
lines = 9
classes = IO::File IO::Dir
lines is the most lines from the call to the line that lets the handle go, nine by default, as in InputOutput::RequireBriefOpen. classes, constructors and methods are those of Perl::Critic::Policy::InputOutput::RequireCheckedIOOpen. Give both policies the same.
METHODS
supported_parameters
lines, and those of Perl::Critic::Policy::InputOutput::RequireCheckedIOOpen.
Returns a list of four HashRefs, the description of each parameter that Perl::Critic reads.
my @parameters = $policy->supported_parameters;
default_severity
High: a handle that stays open holds its file, its lock or its socket.
Returns Int, the $SEVERITY_HIGH of Perl::Critic::Utils.
my $severity = $policy->default_severity;
default_themes
maintenance.
Returns a list of strings, the names of the themes.
my @themes = $policy->default_themes;
violates
One violation for each handle that is not let go within lines lines of the call that opens it, as "Let go in time" says.
Returns a list of Perl::Critic::Violation objects: one when the word is the method of such a call, and none otherwise.
my @violations = $policy->violates($word);
FUNCTIONS
The steps of violates, for its tests.
handle_of
my $variable = handle_of($first);
The scalar that a call opens, from the first element of the call, which "opener_at" in Perl::Critic::Policy::InputOutput::RequireCheckedIOOpen returns: $fh for my $fh = IO::File->new(...) and for $fh->open(...).
Returns Str, the name of the variable with its sigil, or undef for a handle that this policy does not follow.
scope_of
my $scope = scope_of( $variable, $elem );
The node whose end destroys a lexical, as seen from an element: the block that declares it with my, or the if or loop statement whose condition does. The declaration is the nearest of the statements that "statements_before" in Perl::Critic::Policy::InputOutput::RequireCheckedIOOpen returns.
Returns a PPI::Node object, or undef when the scope never ends in time: the variable is a lexical of the file, a state or package variable, or declared nowhere that can be seen.
let_go_within
my $let_go = let_go_within( $variable, $scope, $opener, $end_line );
Whether anything in a scope, after the statement of the opener and by the line $end_line, closes the handle, returns or takes it over, as "Let go in time" says.
Returns 1 when something does, and 0 otherwise.
lets_go
Whether one element lets a handle go. "let_go_within" asks it of each element in range.
Returns 1 when the element lets the handle go, and 0 otherwise.
my $let_go = lets_go( '$fh', $elem );
BUGS
Please report any bugs or feature requests on the bugtracker website https://github.com/Troglodyne-Internet-Widgets/perl-critic-policy-requirebriefioopen/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
AUTHORS
Current Maintainers:
George S. Baugh <george@troglodyne.net>
CONTRIBUTOR
troglodyne-robot <trogbot@troglodyne.net>
COPYRIGHT AND LICENSE
Copyright (c) 2026 Troglodyne LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.