Errno

use Errno;
unless (open(FH, $pathname)) {
    if ($!{ENOENT}) {  # We don't need an import for this!
        warn "$pathname does not exist\n";
    }
    else {
        warn "open failed on `$pathname': $!";
    }
}

use Errno qw(EINTR EIO :POSIX);
if ($! == ENOENT) { …. }

The Errno module makes available the symbolic names for the error status values set when a syscall fails, but does not export them by default. The module has a single export tag, ":POSIX", which exports only those symbols defined by the POSIX 1003.1 standard. The module also makes the global %! variable magical using tie. You can subscript into the %! hash using any valid errno on your system, not just the POSIX ones, and its value is true only if that's the current error.

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.