我在 Bash 脚本中调用许多 Perl 脚本(有时也从 csh 调用)。
在 Bash 脚本的开头,我想进行一个测试,检查所有 Perl 脚本是否没有任何编译错误。
一种方法是从 Bash 脚本中实际调用 Perl 脚本,并在管道日志文件中调用 grep 来查找“编译错误”,但这会变得困惑,因为调用了不同的 Perl 脚本在代码中的不同点,所以我想在 Bash 脚本的一开始就执行此操作。
有没有办法检查Perl脚本是否有编译错误?
最佳答案
小心!!
使用以下命令检查 Perl 程序中的编译错误可能很危险。
$ perl -c yourperlprogram
Randal 就这个主题写了一篇非常好的文章,您应该看看
- Sanity-checking your Perl code (Linux 杂志专栏 91,2007 年 3 月)
引用他的文章:
Probably the simplest thing we can tell is "is it valid?". For this, we invoke perl itself, passing the compile-only switch:
perl -c ourprogram
For this operation, perl compiles the program, but stops just short of the execution phase. This means that every part of the program text is translated into the internal data structure that represents the working program, but we haven't actually executed any code. If there are any syntax errors, we're informed, and the compilation aborts.
Actually, that's a bit of a lie. Thanks to BEGIN blocks (including their layered-on cousin, the use directive), some Perl code may have been executed during this theoretically safe "syntax check". For example, if your code contains:
BEGIN { warn "Hello, world!\n" }
then you will see that message, even during perl -c! This is somewhat surprising to people who consider "compile only" to mean "executes no code". Consider the code that contains:
BEGIN { system "rm", "-rf", "/" }
and you'll see the problem with that argument. Oops.
关于perl - 如何检查 Perl 脚本是否有任何编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12908416/