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
andfork
with functions that die on failure. To get it to work withsystem
, you need to import:all
or:system
, since the default does not do so.It's important to note that for
autodie
to work withsystem
, you need theIPC::System::Simple
module. Install it with CPAN, or on Ubuntu you cansudo apt-get install libipc-system-simple-perl
.