logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles - Write "open my $fh, q{<}, $filename;"

Affiliation

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

Author

       Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>

Configuration

       This Policy is not configurable except for the standard options.

Description

       Using bareword symbols to refer to file handles is particularly evil because they are global, and you
       have no idea if that symbol already points to some other file handle.  You can mitigate some of that risk
       by "local"izing the symbol first, but that's pretty ugly.  Since Perl 5.6, you can use an undefined
       scalar variable as a lexical reference to an anonymous filehandle.  Alternatively, see the IO::Handle or
       IO::File or FileHandle modules for an object-oriented approach.

           open FH, '<', $some_file;           #not ok
           open my $fh, '<', $some_file;       #ok
           my $fh = IO::File->new($some_file); #ok

       There are three exceptions: STDIN, STDOUT and STDERR.  These three standard filehandles are always
       package variables.

       This policy also applies to the "sysopen" function as well.

Name

       Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles - Write "open my $fh, q{<}, $filename;"
       instead of "open FH, q{<}, $filename;".

See Also

       IO::Handle

       IO::File

See Also