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

Perl::Critic::Policy::InputOutput::RequireCheckedOpen3pm

Write "my $error = open $fh, $mode, $filename;" instead of "open $fh, $mode, $filename;".

Affiliation

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

Overview

The perl builtin I/O function "open" returns a false value on failure. That value should always be checked to ensure that the open was successful.

    my $error = open( $filehandle, $mode, $filename );                  # ok
    open( $filehandle, $mode, $filename ) or die "unable to open: $!";  # ok
    open( $filehandle, $mode, $filename );                              # not ok
    use autodie;
    open $filehandle, $mode, $filename;                                 # ok

You can use autodie, Fatal, or Fatal::Exception to get around this. Currently, autodie is not properly treated as a pragma; its lexical effects aren't taken into account.

Configuration

If you create a module that exports "autodie" you can tell this policy about it with the "autodie_modules" setting:

    [InputOutput::RequireCheckedSyscalls]
    autodie_modules = My::Thing

Acknowledgments

This policy module is based heavily on policies written by Jeffrey Ryan Thalhammer <[email protected]>.

Releases

10 releases since Debian 5.0 (Lenny); the text changed 9 times.

  1. 5.0
  2. 6.0
  3. 7
  4. 8
  5. 9
  6. 10
  7. 11
  8. 12
  9. 13
  10. Arch

Author

Andrew Moore <[email protected]>

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