perl equivalent of sh -e

In /bin/sh and /bin/bash (and I guess a lot of other shells), starting scripts with #!/bin/sh -e (or executing set -e in someplace in the script) would cause the script to abort when any command line inside the script exits with a status code different than 0.

Is there any equivalent or workaround to get the same behavior in a perl script? (i.e. if any instruction generates an error or if any external command executed with system(...) or backticks returns error code then exit immediately)


Look at the autodie core module. This replaces calls like open and fork with functions that die on failure. To get it to work with system, you need to import :all or :system, since the default does not do so.

use strict;   #always!
use warnings; #always!
use autodie qw(:system);

system('/bin/false'); #This will die
print "This will never be printed\n";

It's important to note that for autodie to work with system, you need the IPC::System::Simple module. Install it with CPAN, or on Ubuntu you can sudo apt-get install libipc-system-simple-perl.