What are the best practices for error handling in Perl?

The more modern approach is to use the Carp standard library.

use Carp;
my $fh;
open $fh, '<', "file.txt" or confess($!);

The main advantage is it gives a stack trace on death.


Unless you've got a more specific idea, then yes you want to die when unexpected things happen.

  • Dying at the failure to open a file and giving the file name is better than the system telling you it can't read from or write to an anonymous undefined.

  • If you're talking about a "script", in general you're talking about a pretty simple piece of code. Not layers that need coordination (not usually). In a Perl Module, there is an attendant idea is that you don't own the execution environment, so either the main software cares and it catches things in an eval, OR it doesn't really care and dying would be fine. However, one you should try at a little more robustness as a module and just pass back undefs or something.

  • You can catch whatever dies (or croaks) in an eval block. And you can do your more specific handling there.

  • But if you want to inspect $! then write that code, and you'll have a more specific resolution.

  • Take a look at the near-universal standard of using strict. That's code that dies on questionable syntax, rather than letting you continue along.

So I think the general idea is: yes, DIE unless you have a better idea of how things should be handled. If you put enough foresight into it, you can be forgiven for the one or two times you don't die, because you know you don't need to.


Since I use Log::Log4perl almost everywhere, I use $logger->logdie instead of die. And if you want to have more control over your exceptions, consider Exception::Class.

It is better to catch your exceptions with Try::Tiny (see its documentation why).


Whether die is appropriate in the middle of the script really depends on what you're doing. If it's only tens of lines, then it's fine. A small tool with a couple hundred lines, then consider confess (see below). If it's a large object-oriented system with lots of classes and interconnected code, then maybe an exception object would be better.

confess in the Carp package:
Often the bug that led to the die isn't on the line that die reports. Replacing die with confess (see Carp package) will give the stack trace (how we got to this line) which greatly aids in debugging.

For handling exceptions from Perl builtins, I like to use autodie. It catches failures from open and other system calls and will throw exceptions for you, without having to do the or die bit. These exceptions can be caught with a eval { }, or better yet, by using Try::Tiny.