Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

share|improve this question
up vote 8 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.