$ perl -pe 1 foo && echo ok
Can't open foo: No such file or directory.
ok
当文件不存在时,我真的希望 perl 脚本失败。当输入文件不存在时,使 -p 或 -n 失败的“正确”方法是什么?
最佳答案
-p switch只是在此循环中包装代码(-e 后面的参数)的快捷方式:
LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}
(-n 是相同的,但没有 continue 块。)
<>
空运算符等价于 readline *ARGV
, 并连续打开每个参数作为要读取的文件。没有办法影响那个隐式打开的错误处理,但你可以使 warning it emits致命(注意,这也会影响与 -i 开关相关的几个警告):perl -Mwarnings=FATAL,inplace -pe 1 foo && echo ok
关于perl - 找不到输入文件时,如何最好地(惯用地)使 perl 脚本(使用 -n/-p 运行)失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59217670/