Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.