perl相当于sh -e

/bin/sh/bin/bash (我想还有很多其他的shell),用#!/bin/sh -e启动脚本(或者在脚本的某个位置执行set -e )会导致脚本中止当脚本中的任何命令行退出时,状态码不是0。

是否有任何等效或解决方法来获得相同的行为在Perl脚本? (即,如果任何指令产生错误,或者如果使用system(...)执行的任何外部命令或反引号返回错误代码,则立即退出)

看看autodie核心模块。 这样可以取代openfork这样的调用,而失败时则会失效。 要使它与system一起工作,您需要导入:all:system ,因为默认不这样做。

 use strict; #always! use warnings; #always! use autodie qw(:system); system('/bin/false'); #This will die print "This will never be printed\n"; 

需要注意的是, autodiesystem一起工作,您需要IPC::System::Simple模块。 用CPAN安装它,或者在Ubuntu上,你可以sudo apt-get install libipc-system-simple-perl