perl - 找不到输入文件时,如何最好地(惯用地)使 perl 脚本(使用 -n/-p 运行)失败?

标签 perl

$ 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/

相关文章:

windows - Windows 上的 KiokuDB

regex - Perl 正则表达式停止负后视从下一个贪婪捕获中夺走

perl - 如何调用单个 perl 脚本以通过不同输入参数的循环并行运行

bash - 每 X 行 Sed,Y 行之间的 GREP

regex - 否定多个词

regex - perl生成字符串来匹配正则表达式

file - 无法在 Perl 中的 WHILE 循环内写入文件

perl - 如何读取以 "\r"或 "\n"结尾的行的无缓冲输入?

perl - 从Perl中的大文件读取特定行

perl - 使用 Perl 检测空目录