Perl::Critic::Policy::InputOutput::ProhibitBarewordDirHandles(3pm)

Perl::Critic::Policy::InputOutput::ProhibitBarewordDirHandles3pm

Write "opendir my $dh, $dirname;" instead of "opendir dh, $dirname;".

Affiliation

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

Overview

Using bareword symbols to refer to directory handles is particularly evil because they are global, and you have no idea if that symbol already points to some other file or directory 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 file handle or directory handle. Alternatively, see the IO::Handle or IO::Dir modules for an object-oriented approach.

    opendir DH, $some_dir;            #not ok
    opendir *DH, $some_dir;           #not ok
    opendir \*DH, $some_dir;          #not ok
    opendir local *DH, $some_dir;     #not ok
    opendir $dh, $some_dir;           #ok
    opendir my $dh, $some_dir;        #ok
    opendir our $dh, $some_dir;       #ok
    opendir local $dh, $some_dir;     #ok
    my $dh = IO::Dir->new($some_dir); #ok

And Perl7 will probably drop support for bareword filehandles.

Configuration

This Policy is not configurable except for the standard options.

Releases

3 releases since Debian 12 (Bookworm); the text changed 2 times.

  1. 12
  2. 13
  3. Arch

Author

Jeffrey Ryan Thalhammer <[email protected]>, "github.com/pali", "github.com/raforg"

From perl-perl-critic 1.156-3 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.